I have vector with following structure like this. Vector with name "data_1"
are numbers between cells "data_1"
and "data_2"
, etc
row name cell meaning
1 data_1
2 3.4
3 2.3
... ...
40 data_2
41 12
... ...
60 data_3
61 63.2
... ...
I want to create array of data frames DF like this. So each vector is in separate data frame.
DF[1]
3.4
2.3
...
DF[2]
12
...
DF[3]
63.2
...
What is the fast way to do it? I have 4 million records, so traditional cycles such as for(i in 1:nrow)
are too slow. Thanks.
Did you mean to have something like this?
library(zoo)
#split dataframe in list of values
df_list <- split(df, na.locf(sapply(df$cell_meaning, function(x) ifelse(grepl("[0-9]+\\.?[0-9]+",x),
NA,
which(df$cell_meaning==x)))))
names(df_list) <- NULL
#remove data_*
final_list <- lapply(df_list, `[`, -1,)
#final output
final_list
final_list[1]
Output is:
> final_list
[[1]]
[1] "3.4" "2.3"
[[2]]
[1] "12"
[[3]]
[1] "63.2"
> final_list[1]
[[1]]
[1] "3.4" "2.3"
Sample data:
df <- structure(list(cell_meaning = c("data_1", "3.4", "2.3", "data_2",
"12", "data_3", "63.2")), .Names = "cell_meaning", class = "data.frame", row.names = c(NA,
-7L))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.