试图合并在单列中重复的观察结果

时间:2019-02-09 00:58:13

标签: r

这是我的数据:

Year Count        Common.name     County
1 1993     0 Spotted Salamander Bennington
2 1993     6 Spotted Salamander Bennington
3 1993    12       Eastern Newt Bennington
4 1993    23       Eastern Newt Bennington
5 1993     1      American Toad Bennington
6 1993     2          Wood Frog Bennington

这就是我希望我的数据显示的样子:

Year Count        Common.name     County
1 1993     6 Spotted Salamander Bennington
2 1993    35       Eastern Newt Bennington
3 1993    97      American Toad Bennington
4 1993     2          Wood Frog Bennington
5 1993   209         Green Frog Bennington
6 1994    78 Spotted Salamander Chittenden

我有1993年至2017年的数据,在不同的日期对不同的县进行了抽样。我想结合给定物种的年份,计数和县。我不知道如何将它们适当地添加在一起。

1 个答案:

答案 0 :(得分:0)

我认为您需要的是aggregate

DAT = read.table(text='Year Count        Common.name     County
1 1993     0 "Spotted Salamander" Bennington
2 1993     6 "Spotted Salamander" Bennington
3 1993    12       "Eastern Newt" Bennington
4 1993    23       "Eastern Newt" Bennington
5 1993     1      "American Toad" Bennington
6 1993     2          "Wood Frog" Bennington',
header=TRUE)

aggregate(DAT$Count, list(DAT$Year, DAT$Common.name, DAT$County), sum)
  Group.1            Group.2    Group.3  x
1    1993      American Toad Bennington  1
2    1993       Eastern Newt Bennington 35
3    1993 Spotted Salamander Bennington  6
4    1993          Wood Frog Bennington  2