What is the most efficient way to cast a list as a data frame?

前端 未结 7 876
悲&欢浪女
悲&欢浪女 2020-11-29 16:55

Very often I want to convert a list wherein each index has identical element types to a data frame. For example, I may have a list:

> my.list
[[1]]
[[1]]         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 17:38

    I think you want:

    > do.call(rbind, lapply(my.list, data.frame, stringsAsFactors=FALSE))
      global_stdev_ppb      range   tok global_freq_ppb
    1         24267673 0.03114799 hello        211592.6
    2         11561448 0.08870838 world       1002043.0
    > str(do.call(rbind, lapply(my.list, data.frame, stringsAsFactors=FALSE)))
    'data.frame':   2 obs. of  4 variables:
     $ global_stdev_ppb: num  24267673 11561448
     $ range           : num  0.0311 0.0887
     $ tok             : chr  "hello" "world"
     $ global_freq_ppb : num  211593 1002043
    

提交回复
热议问题