从列表到保留列名的数据框

时间:2018-08-30 16:42:57

标签: r list dataframe

[[29]]
                type              lineNum                  cat                  sel 
                 "I"                 "25"                "DOR"               "MN-A" 
                 act                 desc          descChanged                 calc 
                 "+" "Door labor minimum"                  "1"                  "1" 
                 qty                 unit         coverageName              replace 
                 "1"                 "EA"           "Dwelling"              "58.58" 
               total                  acv          recoverable             acvTotal 
             "58.58"              "58.58"                  "1"              "58.58" 
            rcvTotal   isPartOfInitSettle           laborTotal            laborBase 
             "58.58"                  "0"              "58.58"              "58.58" 

[[30]]
                  type                lineNum                    cat                    sel 
                   "I"                   "26"                  "WDW"                 "MN-A" 
                   act                   desc            descChanged                   calc 
                   "+" "Window labor minimum"                    "1"                    "1" 
                   qty                   unit           coverageName                replace 
                   "1"                   "EA"             "Dwelling"                 "69.5" 
                 total                    acv            recoverable               acvTotal 
                "69.5"                 "69.5"                    "1"                 "69.5" 
              rcvTotal     isPartOfInitSettle                          laborBase 
                "69.5"                    "0"                                 "69.5" 

这是我的数据样本。列数不相同。我正在尝试从中创建一个数据框架。我本质上想要两行,并按列名进行匹配。

我尝试使用plyr,但由于列数不相同,因此无法使用。

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

我们可以使用map中的purrr

library(dplyr)
library(purrr)
map_df(lst, as.list)
# A tibble: 3 x 5
#      A     B     C     D     E
#  <int> <int> <int> <int> <int>
#1     1     2     3    NA    NA
#2     1     2    NA    NA    NA
#3     1     2     3     4     5

数据

lst <- list(setNames(1:3, LETTERS[1:3]), setNames(1:2, LETTERS[1:2]), 
            setNames(1:5, LETTERS[1:5]))

答案 1 :(得分:2)

我刚发现

ID

完成工作