合并data.frames以求和R中相同列的值 [英] Combine data.frames summing up values of identical columns in R
问题描述
我在站点内有3个数据框(行:站点,列:物种名称).行号相同,但列号不同,因为在所有三个数据帧中并非所有种类都相同.我想将它们合并到一个数据框中,并总结出大量相同的物种.例如:
I have 3 data frames (rows: sites, columns:species name) of species abundances within sites. Row numbers are identical, but column numbers differ as not all species are in all three data frames. I would like to merge them into one data frame with abundances of identical species summed up. For example:
data.frame1
data.frame1
Sp1 Sp2 Sp3 Sp4
site1 1 2 3 1
site2 0 2 0 1
site3 1 1 1 1
data.frame2
data.frame2
Sp1 Sp2 Sp4
site1 0 1 2
site2 1 2 0
site3 1 1 1
data.frame3
data.frame3
Sp1 Sp2 Sp5 Sp6
site1 0 1 1 1
site2 1 1 1 5
site3 2 0 0 0
我想要的东西是这样的:
What I want to have is something like:
Sp1 Sp2 Sp3 Sp4 Sp5 Sp6
site1 1 4 3 3 1 1
site2 2 5 0 1 1 5
site3 4 2 1 2 0 0
我想我必须使用合并,但是到目前为止,我的尝试未能获得我想要的.
I guess i'd have to work with merge, but so far my attempts have failed to get what I want.
感谢您的帮助.
推荐答案
我会这样使用plyr
的rbind.fill
:
pp <- cbind(names=c(rownames(df1), rownames(df2), rownames(df3)),
rbind.fill(list(df1, df2, df3)))
# names Sp1 Sp2 Sp3 Sp4 Sp5 Sp6
# 1 site1 1 2 3 1 NA NA
# 2 site2 0 2 0 1 NA NA
# 3 site3 1 1 1 1 NA NA
# 4 site1 0 1 NA 2 NA NA
# 5 site2 1 2 NA 0 NA NA
# 6 site3 1 1 NA 1 NA NA
# 7 site1 0 1 NA NA 1 1
# 8 site2 1 1 NA NA 1 5
# 9 site3 2 0 NA NA 0 0
然后,与plyr's
ddply
进行聚合,如下所示:
Then, aggregate with plyr's
ddply
as follows:
ddply(pp, .(names), function(x) colSums(x[,-1], na.rm = TRUE))
# names Sp1 Sp2 Sp3 Sp4 Sp5 Sp6
# 1 site1 1 4 3 3 1 1
# 2 site2 2 5 0 1 1 5
# 3 site3 4 2 1 2 0 0
这篇关于合并data.frames以求和R中相同列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!