Home Ask Login Register

Developers Planet

Your answer is one click away!

kleanBean February 2016

list variables to individual data.frames

Let's say I have a list of 30 data.frames, each containing 2 variables (called value, and rank), called myList

I'd know I can use

my.DF <- do.call("cbind", myList)

to create the output my.DF containing all the variables next to each other.

It is possible to cbind each variable individually into it's own data.frame i.e to just have a new data.frame of just the 2nd variable?


akrun February 2016

We can extract the second column by looping over the list (lapply) and wrap with data.frame.

data.frame(lapply(myList, `[`, 2))

If we want to separate the variables,

lapply(names(myList[[1]]), function(x) 
          do.call(cbind,lapply(myList, `[`, x)))


myList <- list( data.frame(value=1:6, rank= sample(6)), 
                data.frame(value=7:12, rank=sample(6)))

Post Status

Asked in February 2016
Viewed 1,543 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life