将变量与多个data.frame属性组合在一起

时间:2013-03-03 14:57:08

标签: r

我有多个变量,其中包含许多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

中相互追加

我还需要做什么?

3 个答案:

答案 0 :(得分: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汇编,而不是作为单独的命名对象然后合并。

答案 1 :(得分:1)

首先要注意的是(看起来)你没有两个data.frames。你拥有的是两个列表,每个列表包含两个data.frames。一个名为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)

答案 2 :(得分:-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>