我可以将类似数据帧的列表合并到一个数据帧中吗? [重复]

时间:2021-10-28 22:56:17

Possible Duplicate:
Recombining a list of Data.frames into a single data frame.

可能重复:将Data.frames列表重新组合到单个数据框中。

I have a dataframe:

我有一个数据帧:

foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)), 
            df2 = data.frame(x=c('d', 'e', 'f'),y = c(4,5,6)))

Can I convert it to a single dataframe of the form:

我可以将其转换为以下形式的单个数据框:

data.frame(x = c('a', 'b', 'c', 'd', 'e', 'f'), y= c(1,2,3,4,5,6))

?

4 个解决方案

#1


60  

do.call("rbind", foo) should do the trick.

do.call(“rbind”,foo)应该这样做。

#2


8  

with plyr:

与plyr:

foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)), 
        df2 = data.frame(x=c('d', 'e', 'f'),y = c(4,5,6)))

library(plyr)
ldply(foo)[,-1]
  x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6

#3


3  

There are several problems with your code.

您的代码有几个问题。

The first is that the assignment statement in the list doesn't work. This needs to be fixed by, for example:

第一个是列表中的赋值语句不起作用。这需要通过以下方式修复:

foo <- list(
        df1 = data.frame(x=c('a', 'b', 'c'), y = c(1,2,3)), 
        df2 = data.frame(x=c('d', 'e', 'f'), y = c(4,5,6))
)

You can then use rbind() to combine the data frames:

然后,您可以使用rbind()来组合数据框:

rbind(foo$df1, foo$df2)

  x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6

But this poses more questions. For example, why do you combine the data frames in a list in the first place. The second is whether you really need to use data frames rather than vectors. Finally, I generally try to avoid rbind() and rather use merge() when combining data frames in this way.

但这会带来更多问题。例如,为什么首先将列表中的数据框组合在一起。第二个是你是否真的需要使用数据帧而不是向量。最后,我通常会尝试避免使用rbind(),而是在以这种方式组合数据帧时使用merge()。

#4


2  

How about merge(foo[[1]], foo[[2]], all = TRUE)

如何合并(foo [[1]],foo [[2]],all = TRUE)

#1


60  

do.call("rbind", foo) should do the trick.

do.call(“rbind”,foo)应该这样做。

#2


8  

with plyr:

与plyr:

foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)), 
        df2 = data.frame(x=c('d', 'e', 'f'),y = c(4,5,6)))

library(plyr)
ldply(foo)[,-1]
  x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6

#3


3  

There are several problems with your code.

您的代码有几个问题。

The first is that the assignment statement in the list doesn't work. This needs to be fixed by, for example:

第一个是列表中的赋值语句不起作用。这需要通过以下方式修复:

foo <- list(
        df1 = data.frame(x=c('a', 'b', 'c'), y = c(1,2,3)), 
        df2 = data.frame(x=c('d', 'e', 'f'), y = c(4,5,6))
)

You can then use rbind() to combine the data frames:

然后,您可以使用rbind()来组合数据框:

rbind(foo$df1, foo$df2)

  x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6

But this poses more questions. For example, why do you combine the data frames in a list in the first place. The second is whether you really need to use data frames rather than vectors. Finally, I generally try to avoid rbind() and rather use merge() when combining data frames in this way.

但这会带来更多问题。例如,为什么首先将列表中的数据框组合在一起。第二个是你是否真的需要使用数据帧而不是向量。最后,我通常会尝试避免使用rbind(),而是在以这种方式组合数据帧时使用merge()。

#4


2  

How about merge(foo[[1]], foo[[2]], all = TRUE)

如何合并(foo [[1]],foo [[2]],all = TRUE)