2012-04-03 16 views
2

我有三个数据帧,第一个(带列标题,但没有行编号)看起来像填充数据集中使用三个“拼图”

ID 1 2 3 
A 12 NA NA 
B NA 7 NA 
C NA NA 22 

第二个可能看起来像

ID 1 2 3 
A NA 6 NA 
B NA NA 29 
C 43 NA NA 

最后,第三模样

ID 1 2 3 
A NA NA 32 
B 5 NA NA 
C NA 2 NA 

第一列是一个ID列和相同的对所有三个数据帧。最后三列表示相同的变量(1,2和3)。观察值A的记录变量1仅在其中一个数据集中。观察值A的记录也是变量2,但它在不同的数据集中。

如何合并这些数据集在一起,得到类似

ID 1 2 3 
A 12 6 32 
B 5 7 29 
C 43 2 22 

我很抱歉,我没有描述这个问题的更好的方法。如果有人能够分享它的术语,那会很棒。

回答

1

动听的名字!这是非常相似R - Vector/ Array Addition

你可以把你的数据转换成一个多维数组再总结或采取跨越“拼图”维度平均:

df1 <- read.table(text="ID 1 2 3 
A 12 NA NA 
B NA 7 NA 
C NA NA 22", header = TRUE) 

df2 <- read.table(text="ID 1 2 3 
A NA 6 NA 
B NA NA 29 
C 43 NA NA", header = TRUE) 

df3 <- read.table(text="ID 1 2 3 
A NA NA 32 
B 5 NA NA 
C NA 2 NA", header = TRUE) 

# gather inputs and remove common ID column 
lists <- list(df1, df2, df3) 
pieces <- lapply(lists, '[', , -1) 

# turn data into a multi-dimensional array 
a <- array(unlist(pieces), dim = c(nrow(df1), 
            ncol(df1) - 1, 
            length(pieces))) 

# compute sums across pieces 
rowSums(a, na.rm = TRUE, dims = 2) 
# [,1] [,2] [,3] 
# [1,] 12 6 32 
# [2,] 5 7 29 
# [3,] 43 2 22 

然后你只剩下粘贴ID列返回。

2

I didn't come up with it但:

merge.new<-function(...,col.ID){ 
    inter<-merge(...) 
    inter<-inter[order(inter[col.ID]),] #merged data sorted by ID 

    #total columns and rows for the target dataframe 
    total.row<-length(unique(inter[[col.ID]])) 
    total.col<-dim(inter)[2] 
    row.ID<-unique(inter[[col.ID]]) 
    target<-matrix(NA,total.row,total.col) 
    target<-as.data.frame(target) 
    names(target)<-names(inter) 

    for (i in 1:total.row){ 
     inter.part<-inter[inter[col.ID]==row.ID[i],] #select all rows with the same ID 
     for (j in 1:total.col){ 
      if (is.na(inter.part[1,j])){ 
       if(is.na(inter.part[2,j])) {target[i,j]=NA} 
       else {target[i,j]=inter.part[2,j]} 
      } 
      else {target[i,j]=inter.part[1,j]} 

     } 
    } 
print(paste("total rows=",total.row)) 
print(paste("total columns=",total.col)) 
return(target) 
} 

如果你的数据被命名为一,二,三:

> one 
    ID 1 2 3 
2 A 12 NA NA 
3 B NA 7 NA 
4 C NA NA 22 
> two 
    ID 1 2 3 
2 A NA 6 NA 
3 B NA NA 29 
4 C 43 NA NA 
> three 
    ID 1 2 3 
2 A NA NA 32 
3 B 5 NA NA 
4 C NA 2 NA 
> merge.new(merge.new(one, two, all=TRUE, col.ID=1), three, all=TRUE, col.ID=1) 
[1] "total rows= 3" 
[1] "total columns= 4" 
[1] "total rows= 3" 
[1] "total columns= 4" 
    ID 1 2 3 
1 A 12 6 32 
2 B 5 7 29 
3 C 43 2 22 
> 
2

我不知道,如果你可以用数据帧做到这一点直接,但它很容易将它们转换为矩阵第一若不:

x <- matrix(c(12,NA,NA,NA,7,NA,NA,NA,22),3,3) 
y <- matrix(c(NA,NA,43,6,NA,NA,NA,29,NA),3,3) 
z <- matrix(c(NA,5,NA,NA,NA,2,32,NA,NA),3,3) 
b <- matrix(0,3,3) 
b[!is.na(x)] <- x[!is.na(x)] 
b[!is.na(y)] <- y[!is.na(y)] 
b[!is.na(z)] <- z[!is.na(z)] 
b 
    [,1] [,2] [,3] 
[1,] 12 6 32 
[2,] 5 7 29 
[3,] 43 2 22