[英]R Data Table: How to combine a list of data frame into single Data table
I have the following list of data frames. 我有以下数据帧列表。 I want to combine them together and store the result in a data table 我想将它们组合在一起并将结果存储在数据表中
A <- data.frame(A=1:3)
B <- data.frame(B=4:6)
AB_List <- list(A,B)
One of the ways to achieve this is 实现这一目标的方法之一是
AB_df <- data.table(do.call("cbind", AB_List))
Is there any other way to directly get the result, without wrapping it explicitly 还有其他方法可以直接获取结果,而无需显式包装
If you have data frames in a list, as.data.table
will work. 如果列表中有数据框,则as.data.table
将起作用。 See example. 参见示例。
require(data.table)
AB_list <- lapply(seq(LETTERS), function(i) {
df <- data.frame(A = (3*i-2):(3*i))
names(df) <- LETTERS[i]
df
})
as.data.table(AB_list)
To get a list of data frames you need to get a vector of data frame names using ls()
and argument pattern
or grep()
. 要获取数据帧列表,您需要使用ls()
和参数pattern
或grep()
获得数据帧名称的向量。 After that you can create a list of data frames. 之后,您可以创建数据帧列表。
df_names <- LETTERS
df_list <- mget(df_names)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.