简体   繁体   中英

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. 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)

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

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 <- 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 <- data <- as.list(1:3) # test data
Names <- c("formtype", "cl", "date")
as.data.frame(lapply(mget(Names, .GlobalEnv), unlist))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM