Rilcon42 Rilcon42 - 3 months ago 9
R Question

Force list of lists into dataframe

I am trying to turn a list of lists into a dataframe, but cant figure it out. Is there a standard function for this or something?

res<-lapply(1:5,function(x){list("RSI_MA"=x*2,"success_rate"=x,"cutoff_value"=(x+9))})
View(res) #WRONG
View(as.matrix(unlist(res),ncol=3,byrow=T)) #WRONG


GOAL

RSI_MA success_rate cutoff_value
2 1 10
4 2 11
6 3 12
8 4 13
10 5 14

Answer

How about

## can use `rbind`
do.call(rbind.data.frame, lapply(res, as.data.frame))

#  RSI_MA success_rate cutoff_value
#1      2            1           10
#2      4            2           11
#3      6            3           12
#4      8            4           13
#5     10            5           14

Actually, this will work too:

## have to use `rbind.data.frame`
do.call(rbind.data.frame, res)