library(magrittr)
tjt <- temp.jimported.tidy
for(y in 2:length(tjt)) {
# start the unnest in col 2
tjt %<>% unnest_longer(col = colnames(tjt[y]),
indices_include = FALSE)}
?
library(magrittr)
tjt <- temp.jimported.tidy
for(y in 2:length(tjt)) {
# start the unnest in col 2
tjt %<>% unnest_longer(col = colnames(tjt[y]),
indices_include = FALSE)}
?
its cleaner -- I will use it!
But honestly, I was thinking of a lapply()
style, where it would do the unnest_longer()
for all the cols
in a given data.frame
where it would piped it self without a for
loop; if it makes any sense.
something to play with...
library(tidyverse)
intext <-'{
"Experiment1_01":[
{
"var1" : ["[[A01]]"],
"var2" : ["2","1","3"],
"var3" : ["0.2", "0.5"]
}]
,
"Experiment2_01":[
{
"var1" : ["[[A03]]"],
"var2" : ["1"],
"var3" : ["0.3","0.5"]
}
]
}'
myt <- tempfile()
writeLines(intext,myt)
jimported <- jsonlite::read_json(myt,
simplifyVector = TRUE)
jimported.df <- tibble()
if (length(jimported) > 1) {
for (k in 1:length(jimported)) {
jimported.df <- bind_rows(jimported.df,
cbind(tibble(exp=names(jimported[k])),jimported[[k]]))
}
} else {
jimported.df <-bind_rows(jimported.df,
cbind(tibble(exp=names(jimported[1])),jimported[[1]]))
}
#unnest all vars
myvars<- names(jimported.df)[startsWith(names(jimported.df),"var")]
walk(myvars,
~assign("jimported.df",
unnest_longer(data=jimported.df,
col = .x),
envir = .GlobalEnv)
)
Nice @nirgrahamuk!
But I prefer your earlier solution. The latter is more to study than to play
But I also like the fact that you include the input: much easier for the reader
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.