-1

我有多个具有许多 data.frame 属性的变量,如下所示:

> df1
$ML1
  Q2 Q3
1  1  1
2  0  0
3  0  1
4  1  1
5  0  0

$ML2
  Q4 Q5
1  0  1
2  1  1
3  1  0
4  1  0
5  0  0

> df2
$ML2
   Q4 Q5
1   1  1
2   1  0
3   1  0
4   0  0
5   0  0
6   1  0
7   1  1
8   0  1
9   1  1
10  0  1

$ML1
   Q2 Q3
1   0  0
2   1  1
3   0  1
4   1  0
5   0  1
6   1  0
7   0  1
8   1  0
9   1  1
10  0  0

我想将它们组合成一个带有 data.frames 的变量,如下所示:

> dfAll
$ML1
  Q2 Q3
1  1  1
2  0  0
3  0  1
4  1  1
5  0  0
6  0  0
7  1  1
8  0  1
9  1  0
10 0  1
11 1  0
12 0  1
13 1  0
14 1  1
15 0  0

$ML2
  Q4 Q5
1  0  1
2  1  1
3  1  0
4  1  0
5  0  0
6  1  1
7  1  0
8  1  0
9  0  0
10 0  0
11 1  0
12 1  1
13 0  1
14 1  1
15 0  1

我已经尝试过rbind(df1, df2),但这会创建一个看起来像这样的变量:

> rbind(df1, df2)[,"ML1"]
$df1
  Q2 Q3
1  1  1
2  0  0
3  0  1
4  1  1
5  0  0

$df2
   Q4 Q5
1   1  1
2   1  0
3   1  0
4   0  0
5   0  0
6   1  0
7   1  1
8   0  1
9   1  1
10  0  1

因此,这些行不会在同一个 data.frame 中相互附加

我还需要做什么?

4

3 回答 3

2

这假设,如在您的示例中,您的所有数据帧列表(df1、df2 等)都是一致的,特别是如果 df1 具有元素“foo”,则 df2 也具有元素“foo”,并且 df1$foo和 df2$foo 是rbind兼容的数据帧。

# create two sample lists of data.frames
df1 <- lapply(list(ML1=1:2, ML2=3:4), function(i) head(iris[i]))
df2 <- lapply(list(ML2=3:4, ML1=1:2), function(i) tail(iris[i]))
# store them in a list for easier reference
dfList <- list(df1, df2)
# for each data frame name, extract and rbind the corresponding data 
dfAll <- sapply(names(dfList[[1]]), function(col) do.call("rbind", 
    lapply(dfList, "[[", col)), simplify=FALSE)

这在精神上与其他答案相似,但会产生问题中提出的输出结构:

> dfAll
$ML1
    Sepal.Length Sepal.Width
1            5.1         3.5
2            4.9         3.0
3            4.7         3.2
4            4.6         3.1
5            5.0         3.6
6            5.4         3.9
145          6.7         3.3
146          6.7         3.0
147          6.3         2.5
148          6.5         3.0
149          6.2         3.4
150          5.9         3.0

$ML2
    Petal.Length Petal.Width
1            1.4         0.2
2            1.4         0.2
3            1.3         0.2
4            1.5         0.2
5            1.4         0.2
6            1.7         0.4
145          5.7         2.5
146          5.2         2.3
147          5.0         1.9
148          5.2         2.0
149          5.4         2.3
150          5.1         1.8

顺便说一句,假设这是更大工作流程的一部分,如果可能的话,我会修改上游代码,以便dfList从一开始就组装数据帧,而不是作为随后组合的单个命名对象。

于 2013-03-03T17:12:34.370 回答
1

首先要注意的是(看起来)你没有两个 data.frames。您拥有的是两个列表,每个列表都包含两个 data.frame。一个名为 ML1,另一个名为 ML2。

#Example data for the purpose of being reproducible
df1 <- list(ML1=data.frame(Q2=1:5,Q3=LETTERS[1:5]),ML2=data.frame(Q4=6:10,Q5=LETTERS[6:10]))
df2 <- list(ML1=data.frame(Q2=11:15,Q3=LETTERS[11:15]),ML2=data.frame(Q4=16:24,Q5=LETTERS[16:24]))
# Lets look at the structure, just for educational purposes
str(df1)
str(df2)
# Okay, now how can we bind those lists together?  
# It turns out, we just use c because lists are really just vectors of type list.
list.all <- c(df1,df2)
# Now that all of the data is in one structure, we have hope.
# The rbindListsByName function does the heavy lifting.  
# Notice we couldn't just provide "nameToBind" otherwise it would just grab the first list with a matching name.  
#We needed a logical to pick out the lists we actually wanted.
rbindListsByName <- function(nameToBind,inputList) {
   do.call("rbind",inputList[names(inputList)==nameToBind])
}
dfAll <- sapply(names(df1),rbindListsByName,inputList=list.all,simplify=FALSE)
于 2013-03-03T16:23:19.210 回答
-1

我会试一试。但是,您的问题仍然无法重现。

#Here I try to recreate your objects:
ML1 <- read.table(text="Q2 Q3
1  1  1
2  0  0
3  0  1
4  1  1
5  0  0",header=TRUE)

ML2 <- read.table(text="Q4 Q5
1  0  1
2  1  1
3  1  0
4  1  0
5  0  0",header=TRUE)

df1 <- list(ML1=ML1,ML2=ML2)

ML2 <- read.table(text="Q4 Q5
1   1  1
2   1  0
3   1  0
4   0  0
5   0  0
6   1  0
7   1  1
8   0  1
9   1  1
10  0  1",header=TRUE)

ML1 <- read.table(text="Q2 Q3
1   0  0
2   1  1
3   0  1
4   1  0
5   0  1
6   1  0
7   0  1
8   1  0
9   1  1
10  0  0",header=TRUE)

df2 <- list(ML2=ML2,ML1=ML1)

我看不出有理由保持ML1ML2分开。因此,我cbind他们。

#combine the lists in a list
mylist <- list(df1,df2)

#cbind the data.frames in each sublist
mylist <- lapply(mylist,function(x) do.call("cbind",x))
#rbind the resulting data.frames
do.call("rbind",mylist)

#   ML1.Q2 ML1.Q3 ML2.Q4 ML2.Q5
#1       1      1      0      1
#2       0      0      1      1
#3       0      1      1      0
#</snip>
于 2013-03-03T16:21:56.997 回答