将数据帧列表转换为数据表 [英] Converting list of data frames to a data table
问题描述
我有一个数据框列表,例如:
listOfDataFrames< - vector(list,10)
for(i in 1:10){
listOfDataFrames [[i]] }我想创建一个具有3列(a,b,c)的单个数据表,其中a和b是上面数据帧的列,c是listOfDataframes中数据帧的索引。解决方案做如下:
library(data.table)
Lens< - vapply(listOfDataframes,nrow, 1L)
rbindlist(listOfDataframes)[,c:= rep(seq_along(Lens),times = Lens)] []
#abc
#1:-1.6462894 1.0232899 1
#2:-0.5145108 -0.2134384 1
#3:-0.1171853 2.5456709 1
#4:0.2735289 1.1948928 1
#5:0.5739892 0.3939964 1
#---
#496:0.9539835 -1.4100199 10
#497:-0.8697604 0.6793800 10
#498:0.8601795 -0.3015890 10
#499:0.8306091 -2.2269960 10
#500:-1.3407596 0.5014448 10
基本上,Lens只是计算出每个列表项中有多少行这种情况下,每个50),然后你只需使用 rep
来计算列c的值。由于 rbindlist
的结果是 data.table
,您只需使用:=
在复合语句中指定c的值。
=https://github.com/Rdatatable/data.table/wiki/Installation =nofollow> 开发版本 ,您现在可以替换 vapply
部分使用新的 idcol
参数。 idcol
始终为字符类型。
rbindlist(listOfDataframes,idcol =c)
I have a list of data frames eg:
listOfDataFrames <- vector("list", 10)
for (i in 1:10) {
listOfDataFrames[[i]] <- data.frame(a=rnorm(50), b=rnorm(50))
}
I want to make a single data table with 3 columns (a,b,c) where a and b are the columns of the data frames above and c is the index of the data frame in listOfDataframes.
解决方案 I would just do something like the following:
library(data.table)
Lens <- vapply(listOfDataframes, nrow, 1L)
rbindlist(listOfDataframes)[, c := rep(seq_along(Lens), times = Lens)][]
# a b c
# 1: -1.6462894 1.0232899 1
# 2: -0.5145108 -0.2134384 1
# 3: -0.1171853 2.5456709 1
# 4: 0.2735289 1.1948928 1
# 5: 0.5739892 0.3939964 1
# ---
# 496: 0.9539835 -1.4100199 10
# 497: -0.8697604 0.6793800 10
# 498: 0.8601795 -0.3015890 10
# 499: 0.8306091 -2.2269960 10
# 500: -1.3407596 0.5014448 10
Basically, "Lens" just figures out how many rows there are in each list item (in this case, 50 in each), and then you just use rep
to calculate the value for column "c". Since the result of rbindlist
is a data.table
, you can just use :=
to assign the value of "c" in a compound statement.
With the development version you can now replace the vapply
part with the new idcol
argument. idcol
is always of character type.
rbindlist(listOfDataframes, idcol = "c")
这篇关于将数据帧列表转换为数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!