Reputation: 9763
I am trying to turn a list of lists into a data.frame
, but can't 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))})
res #WRONG
as.matrix(unlist(res),ncol=3,byrow=T) #WRONG
I would like the output to look like
RSI_MA success_rate cutoff_value
2 1 10
4 2 11
6 3 12
8 4 13
10 5 14
Upvotes: 3
Views: 639
Reputation: 1648
dplyr::bind_rows()
works super well for these things.
res %>% bind_rows()
# A tibble: 5 x 3
RSI_MA success_rate cutoff_value
<dbl> <int> <dbl>
1 2.00 1 10.0
2 4.00 2 11.0
3 6.00 3 12.0
4 8.00 4 13.0
5 10.0 5 14.0
Upvotes: 3
Reputation: 73385
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)
Upvotes: 2