我根据数据框创建了一个变量,将物种群描述为国内、野生或外来物种,其中每一行代表在唯一站点 (siteID) 中发现的物种。我想通过每个 siteID 将行插入到我的数据框中,以报告在该站点未观察到的组的 0。换句话说,这是我拥有的数据框:
df.start <- data.frame(species = c("dog","deer","toucan","dog","deer","toucan"),
siteID = c("a","b","b","c","c","c"),
group = c("domestic", "wild", "exotic", "domestic", "wild", "exotic"),
value = c(2:7))
df.start
# species siteID group value
# 1 dog a domestic 2
# 2 deer b wild 3
# 3 toucan b exotic 4
# 4 dog c domestic 5
# 5 deer c wild 6
# 6 toucan c exotic 7
这是我想要的数据框:
df.end <-data.frame(species=c("dog","NA","NA","NA","deer",
"toucan","dog","deer","toucan"),
siteID = c("a","a","a","b","b","b","c","c","c"),
group = rep(c("domestic", "wild", "exotic"),3),
value = c(2,0,0,0,3,4,5,6,7))
df.end
# species siteID group value
# 1 dog a domestic 2
# 2 NA a wild 0
# 3 NA a exotic 0
# 4 NA b domestic 0
# 5 deer b wild 3
# 6 toucan b exotic 4
# 7 dog c domestic 5
# 8 deer c wild 6
# 9 toucan c exotic 7
这是因为我想使用 plyr 函数按组汇总平均值,并且我意识到某些组站点组合缺少零并且夸大了我的估计。也许我错过了一个更明显的解决方法?