将向量列表转换为数据框

时间:2017-04-27 15:50:22

标签: r list vector dataframe

我正在尝试将矢量列表(本质上是一个多维数组)转换为数据帧,但每次尝试时我都会得到意想不到的结果。

我的目标是实例化一个空白列表,在for循环中填充包含循环迭代信息的向量,然后在完成后将其转换为数据框。

> vectorList <- list()
> for(i in  1:5){
+     vectorList[[i]] <- c("number" = i, "square root" = sqrt(i))
+ }
> vectorList

输出:

> [[1]]
>      number square root 
>           1           1 
> 
> [[2]]
>      number square root 
>    2.000000    1.414214 
> 
> [[3]]
>      number square root 
>    3.000000    1.732051 
> 
> [[4]]
>      number square root 
>           4           2 
> 
> [[5]]
>      number square root 
>    5.000000    2.236068

现在我希望这成为一个包含2个变量的5个观察数据的数据框,但是试图从'vectorList'创建一个数据框

numbers <- data.frame(vectorList)

导致5个变量的2次观察。

奇怪的是它甚至不会被重塑(我知道这将是一个糟糕的工作,但我尝试过)。

有人有任何见解吗?

4 个答案:

答案 0 :(得分:25)

您可以使用:

as.data.frame(do.call(rbind, vectorList))

或者:

library(data.table)
rbindlist(lapply(vectorList, as.data.frame.list))

或者:

library(dplyr)
bind_rows(lapply(vectorList, as.data.frame.list))

答案 1 :(得分:8)

我知道最快最有效的方法是使用data.table::transpose函数(如果向量的长度是低维的):

as.data.frame(data.table::transpose(vectorList), col.names = names(vectorList[[1]]))

但是,您需要手动设置列名称,因为data.table::transpose会删除它们。还有一个purrr::transpose函数不会删除列名,但它似乎更慢。 低于一个小基准,包括其他用户的建议:

vectorList = lapply(1:1000, function(i) (c("number" = i, "square root" = sqrt(i))))
bench = microbenchmark::microbenchmark(
  dplyr = dplyr::bind_rows(lapply(vectorList, as.data.frame.list)),
  rbindlist = data.table::rbindlist(lapply(vectorList, as.data.frame.list)),
  Reduce = Reduce(rbind, vectorList),
  transpose_datatable = as.data.frame(data.table::transpose(vectorList), col.names = names(vectorList[[1]])),
  transpose_purrr = data.table::as.data.table(purrr::transpose(vectorList)),
  do.call = as.data.frame(do.call(rbind, vectorList)),
  times = 10)
bench
# Unit: microseconds
#                 expr        min         lq        mean      median         uq        max neval cld
#                dplyr 286963.036 292850.136 320345.1137 310159.7380 341654.619 385399.851    10   b
#            rbindlist 285830.750 289935.336 306120.7257 309581.1895 318131.031 324217.413    10   b
#               Reduce   8573.474   9073.649  12114.5559   9632.1120  11153.511  33446.353    10  a 
#  transpose_datatable    372.572    424.165    500.8845    479.4990    532.076    701.822    10  a 
#      transpose_purrr    539.953    590.365    672.9531    671.1025    718.757    911.343    10  a 
#              do.call    452.915    537.591    562.9144    570.0825    592.334    641.958    10  a 

# now use bigger list and disregard the slowest
vectorList = lapply(1:100000, function(i) (c("number" = i, "square root" = sqrt(i))))
bench.big = microbenchmark::microbenchmark(
  transpose_datatable = as.data.frame(data.table::transpose(vectorList), col.names = names(vectorList[[1]])),
  transpose_purrr = data.table::as.data.table(purrr::transpose(vectorList)),
  do.call = as.data.frame(do.call(rbind, vectorList)),
  times = 10)
bench.big
# Unit: milliseconds
#                 expr       min        lq       mean     median         uq       max neval cld
#  transpose_datatable  3.470901   4.59531   4.551515   4.708932   4.873755   4.91235    10 a  
#      transpose_purrr 61.007574  62.06936  68.634732  65.949067  67.477948  97.39748    10  b 
#              do.call 97.680252 102.04674 115.669540 104.983596 138.193644 151.30886    10   c

答案 2 :(得分:6)

另外Reduce

Reduce(rbind, vectorList)

    # number square root
# init      1    1.000000
          # 2    1.414214
          # 3    1.732051
          # 4    2.000000
          # 5    2.236068

答案 3 :(得分:2)

使用purrr的替代解决方案:

purrr::map_dfr( vectorList, as.list )
# # A tibble: 5 x 2
#   number `square root`
#    <dbl>         <dbl>
# 1      1          1   
# 2      2          1.41
# 3      3          1.73
# 4      4          2   
# 5      5          2.24

该代码有效地将每个向量转换为一个列表,并将结果按行连接到一个公共数据帧中。