[英]Convert large number of lists into variables in a data frame
I have a couple hundred lists of length ~4000 that I want to combine into a data frame with each list becoming a variable in the data frame. 我有几百个长度约为4000的列表,我想将它们合并到一个数据帧中,每个列表都成为数据帧中的变量。 How can I do this while preserving the names of the lists as the variable names in the new data frame? 如何在将列表名称保留为新数据框中的变量名称的同时执行此操作?
Here is what I have so far with the first 3 lists (formtype, cl, and date) 这是到目前为止我对前3个列表(formtype,cl和date)的了解
datalist = list()
datalist[[1]] <- formtype
datalist[[2]] <- cl
datalist[[3]] <- date
data.out = as.data.frame(do.call(cbind,datalist))
However, this does not preserve the list names (formtype,cl,date,etc...) as variable names in data.out 但是,这不会将列表名称(formtype,cl,date等)保留为data.out中的变量名称。
Assuming that formtype
, cl
and date
are ordinary vectors (not lists) and that they are in the global environment, ie in workspace, then: 假设formtype
, cl
和date
是普通向量(不是列表),并且它们在全局环境中,即在工作空间中,则:
formtype <- cl <- date <- 1:3 # test data
Names <- c("formtype", "cl", "date")
as.data.frame(mget(Names, .GlobalEnv))
giving: 给予:
formtype cl date
1 1 1 1
2 2 2 2
3 3 3 3
If formtype
, cl
and date
really are lists as shown below then unlist
each: 如果formtype
, cl
和date
确实是如下所示的列表,则unlist
列出每个列表:
formtype <- cl <- data <- as.list(1:3) # test data
Names <- c("formtype", "cl", "date")
as.data.frame(lapply(mget(Names, .GlobalEnv), unlist))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.