将列表转换为data.frame的更快方法,其中缺少某些列值

时间:2017-02-01 14:23:17

标签: r data-manipulation

我有这份清单

> head(train)
[[1]]
[[1]]$Physics
[1] 8

[[1]]$Chemistry
[1] 7

[[1]]$PhysicalEducation
[1] 3

[[1]]$English
[1] 4

[[1]]$Mathematics
[1] 6

[[1]]$serial
[1] 195490

.
.
[[6]]
[[6]]$Physics
[1] 2

[[6]]$Chemistry
[1] 1

[[6]]$Biology
[1] 2

[[6]]$English
[1] 4

[[6]]$Mathematics
[1] 8

[[6]]$serial
[1] 182318

每个子列表中包含12个中的任意五个元素,另外一个名为serial

columns <- c("Physics", "Chemistry", "PhysicalEducation", "English", 
             "Mathematics", "serial", "ComputerScience", "Hindi", "Biology", 
             "Economics", "Accountancy", "BusinessStudies")

我正在尝试将此列表转换为数据框。

目前,我通过一次迭代一行来使用这个for循环。虽然这有效,但需要花费大量时间。

colclass <- rep("numeric",12)
comby <- read.table(text = '', colClasses = colclass, col.names = columns)  
for(i in 1:length(train)){
    comby[i,names(train[[i]])] <- train[[i]]
}

我尝试使用do.call(rbind, train),但这不起作用,因为它会在第一次迭代中不断向旧列中添加新数据。

什么是更好,更快的方式?我有大约150万次观察 所需的o / p :数据框应包含所有列。我希望NA在没有价值的地方。另外,我感兴趣的是如果可以在不使用任何其他包的情况下更快地完成。

 Physics Chemistry PhysicalEducation English Mathematics serial ComputerScience Hindi Biology Economics Accountancy
1       8         7                 3       4           6 195490              NA    NA      NA        NA          NA
2       1         1                 1       3           3 190869              NA    NA      NA        NA          NA
3       1         2                 2       1           2   3111              NA    NA      NA        NA          NA
4       8         7                 6       7           7  47738              NA    NA      NA        NA          NA
5       1         1                 1       3           2  85520              NA    NA      NA        NA          NA
6       2         1                NA       4           8 182318              NA    NA       2        NA          NA
  BusinessStudies
1              NA
2              NA
3              NA
4              NA
5              NA
6              NA

这是可重现的代码

train <- [{\"Physics\":8,\"Chemistry\":7,\"PhysicalEducation\":3,\"English\":4,\"Mathematics\":6,\"serial\":195490},{\"Physics\":1,\"Chemistry\":1,\"PhysicalEducation\":1,\"English\":3,\"Mathematics\":3,\"serial\":190869},{\"Physics\":1,\"Chemistry\":2,\"PhysicalEducation\":2,\"English\":1,\"Mathematics\":2,\"serial\":3111},{\"Physics\":8,\"Chemistry\":7,\"PhysicalEducation\":6,\"English\":7,\"Mathematics\":7,\"serial\":47738},{\"Physics\":1,\"Chemistry\":1,\"PhysicalEducation\":1,\"English\":3,\"Mathematics\":2,\"serial\":85520},{\"Physics\":2,\"Chemistry\":1,\"Biology\":2,\"English\":4,\"Mathematics\":8,\"serial\":182318},{\"Physics\":3,\"Chemistry\":4,\"PhysicalEducation\":5,\"English\":5,\"Mathematics\":8,\"serial\":77482},{\"Accountancy\":2,\"BusinessStudies\":5,\"Economics\":3,\"English\":6,\"Mathematics\":7,\"serial\":152940},{\"Physics\":5,\"Chemistry\":6,\"Biology\":7,\"English\":3,\"Mathematics\":8,\"serial\":132620}]
train <- rjson::fromJSON(train)

2 个答案:

答案 0 :(得分:1)

您可以通过合并ReduceMap来完成此基础。

数据

这是一个与您的结构相匹配的数据集。

set.seed(1234)
temp <- replicate(7, setNames(replicate(7, sample(1:10, 1), simplify=FALSE), letters[1:7]),
                  simplify=FALSE)

要从中生成data.frame,您可以使用

Reduce(rbind, Map(data.frame, temp))
  a b c  d e f  g
1 2 7 7  7 9 7  1
2 3 7 6  7 6 3 10
3 3 9 3  3 2 3  4
4 4 2 1  3 9 6 10
5 9 1 5  3 4 6  2
6 8 3 3 10 9 6  7
7 4 7 4  6 7 5  3

其中data.frame使用内部元素构造data.frames。 Map将此值应用于外部列表的每个元素,从而生成data.frames列表。最后,Reduce rbind列表中的data.frames并生成一个data.frame。

答案 1 :(得分:1)

作为起点,您可以使用purrr :: map,如下所示:

示例数据集:

x <- list(list(physics=8,
               Chemistry=7,
               PhysicalEducation=3,
               English=4,
               serial=195490),
          list(physics=2,
               Chemistry=1,
               Biology=2,
               English=4,
               Mathematics=8,
               serial=182318))

Sol.1 [最短的避免循环]

zzz <- sapply(columns, function(n) map_dbl(x,n,.null=NA) ) %>% 
        data.frame()

给出了:

> zzz
  Physics Chemistry PhysicalEducation English Mathematics serial ComputerScience Hindi Biology Economics
1      NA         7                 3       4          NA 195490              NA    NA      NA        NA
2      NA         1                NA       4           8 182318              NA    NA       2        NA
  Accountancy BusinessStudies
1          NA              NA
2          NA              NA

如果您想了解其工作原理,可以查看以下较长的解决方案。

Sol.2 [手动分配]

- 选择每列的值:

z <- data.frame(
    serial = map_dbl(x,"serial",.null=NA),
    Biology = map_dbl(x,"Biology",.null=NA),
    Chemistry = map_dbl(x,"Chemistry",.null=NA)
        )

给出了:

> z
  serial Biology Chemistry
1 195490      NA         7
2 182318       2         1
>

Sol.3 [预定义的数据框和for-loop]

  • 创建一个固定大小的数据框

    zz <- data.frame(matrix(NA, nrow = length(x), ncol = 12))

  • 指定名称

    names(zz) <- columns

  • 从列表中分配值

    for(i in 1:ncol(zz)){ zz[columns[i]] <- map_dbl(x,columns[i],.null=NA) }

给出了:

> zz
  Physics Chemistry PhysicalEducation English Mathematics serial ComputerScience Hindi Biology Economics
1      NA         7                 3       4          NA 195490              NA    NA      NA        NA
2      NA         1                NA       4           8 182318              NA    NA       2        NA
  Accountancy BusinessStudies
1          NA              NA
2          NA              NA