0

我根据数据框创建了一个变量,将物种群描述为国内、野生或外来物种,其中每一行代表在唯一站点 (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 函数按组汇总平均值,并且我意识到某些组站点组合缺少零并且夸大了我的估计。也许我错过了一个更明显的解决方法?

4

2 回答 2

1

使用基本 R 函数:

result <- merge(  
  with(df.start, expand.grid(siteID=unique(siteID),group=unique(group))),
  df.start,
  by=c("siteID","group"),
  all.x=TRUE
)
result$value[is.na(result$value)] <- 0

> result
  siteID    group species value
1      a domestic     dog     2
2      a   exotic    <NA>     0
3      a     wild    <NA>     0
4      b domestic    <NA>     0
5      b   exotic  toucan     4
6      b     wild    deer     3
7      c domestic     dog     5
8      c   exotic  toucan     7
9      c     wild    deer     6
于 2013-07-17T05:41:25.703 回答
1
 df.sg <- data.frame(xtabs(value~siteID+group, data=df.start))
 merge(df.start[-4], df.sg, by=c("siteID", "group"), all.y=TRUE)
#-------------
  siteID    group species Freq
1      a domestic     dog    2
2      a   exotic    <NA>    0
3      a     wild    <NA>    0
4      b domestic    <NA>    0
5      b   exotic  toucan    4
6      b     wild    deer    3
7      c domestic     dog    5
8      c   exotic  toucan    7
9      c     wild    deer    6 

xtabs函数返回一个表,该表允许该as.data.frame.table方法对其进行处理。非常便利。

于 2013-07-17T05:48:02.807 回答