【发布时间】:2018-09-19 00:04:16
【问题描述】:
我对 R 相当陌生,我有一个数据框,其中包含在各个站点检测到的个体鸟类的数量,以及每个个体的繁殖证据(有序因素)。它看起来像这样:
station<-c('stn1','stn1','stn1','stn1','stn1','stn2','stn2')
species<-c('PBGR','PBGR','SORA','SORA','SORA','LCSP','SORA')
interval1<-c(0,0,1,1,0,1,0)
interval2<-c(1,1,0,0,1,0,1)
breeding.evidence<-c('A','A','S','S','D','S','S')
breeding.evidence<-factor(breeding.evidence,levels=c('S','D','A'))
df<-data.frame(station,species,interval1,interval2,breeding.evidence)
>df
station species interval1 interval2 breeding.evidence
1 stn1 PBGR 0 1 A
2 stn1 PBGR 0 1 S
3 stn1 SORA 1 0 S
4 stn1 SORA 1 0 S
5 stn1 SORA 0 1 D
6 stn2 LCSP 1 0 S
7 stn2 SORA 0 1 S
事实上,每一行代表一个人。我想总结数据框为同一物种的记录添加interval1和interval2,并选择最高的育种证据。到目前为止,我有:
library(dplyr)
df%>%
group_by(station,species)%>%
summarise_at(vars(interval1:interval2),sum)
哪个有效,然后返回
station species interval1 interval2
1 stn1 PBGR 0 2
2 stn1 SORA 2 1
3 stn2 LCSP 1 0
4 stn2 SORA 0 1
但是我已经失去了繁殖证据,并且尽我所能尝试我无法弄清楚什么代码可以工作。我最后想要的是这样的:
station species interval1 interval2 breeding.evidence
1 stn1 PBGR 0 2 A
2 stn1 SORA 2 1 D
3 stn2 LCSP 1 0 S
4 stn2 SORA 0 1 S
提前感谢您的帮助!
【问题讨论】:
-
breeding.evidence在您的实际数据集中是否有相同的 3 个级别? -
在您的数据集中相同的
station和species属于两个不同的breeding.evidence(1 stn1 PBGR 0 1 A2 stn1 PBGR 0 1 S)。所以我不确定你如何才能得到你的结果。