Rbind list of vectors with differing lengths

后端 未结 2 542
失恋的感觉
失恋的感觉 2021-01-25 05:31

I am new to R and I am trying to build a frequency/severity simulation. Everything is working fine except that it takes about 10min to do 10000 simulations for each of 700 locat

2条回答
  •  伪装坚强ぢ
    2021-01-25 06:35

    We can append NAs at the end to make the length same for each of the list elements and then do the rbind

    out <- do.call(rbind, lapply(obs, `length<-`, max(lengths(obs))))
    as.data.frame(out) # if we need a data.frame as output
    

    or using tidyverse

    library(tidyverse)
    obs %>%
       set_names(seq_along(.)) %>% 
       stack %>% 
       group_by(ind) %>% 
       mutate(Col = paste0("Col", row_number())) %>% 
       spread(Col, values)
    

提交回复
热议问题