子组

时间:2016-03-18 18:46:13

标签: r dplyr

我有以下数据框:

df<-data.frame(Date=rep(x=as.Date(c("2016/01/01", "2016/01/02")),each=12),
       Group=rep(x=c("G1","G2"),each=6,times=2),
       ID=rep(c("G1F1","G1F2","G1F3","G1M4","G1M5","G1M6","G2F1","G2F2","G2F3","G2M4","G2M5","G2M6"),times=2),
       Gender=rep(c("Female","Male"),each=3,times=4),
       Weight=c(c(100,100,100,100,120,140),rep(c(100,120,140,100,100,100),times=2),c(100,100,100,100,120,140)))

我想创建一个新列CountComp,显示相同性别的计数相同的组成员计数,其权重在给定的每行中指定的个体的重量之上或之下10克以内得到的日期:

df2<-data.frame(Date=rep(x=as.Date(c("2016/01/01", "2016/01/02")),each=12),
           Group=rep(x=c("G1","G2"),each=6,times=2),
           ID=rep(c("G1F1","G1F2","G1F3","G1M4","G1M5","G1M6","G2F1","G2F2","G2F3","G2M4","G2M5","G2M6"),times=2),
           Gender=rep(c("Female","Male"),each=3,times=4),
           Weight=c(c(100,100,100,100,120,140),rep(c(100,120,140,100,100,100),times=2),c(100,100,100,100,120,140)),
           CountComp=c(c(2,2,2,0,0,0),rep(c(0,0,0,2,2,2),times=2),c(2,2,2,0,0,0)))

我是R的新手并且认为dplyr可以使用group_bymutate函数提供解决方案,但到目前为止找不到它。

1 个答案:

答案 0 :(得分:3)

这样的事情可以起作用

df %>% group_by(Date, Group, Gender) %>% 
    mutate(CountComp=sapply(Weight, function(x) sum(abs(x-Weight)<10)-1))

在这里,我们执行group_by以获取您想要获得计数的类。然后我们使用mutate来获取新列。我们需要将每个值与组中的每个其他值进行比较。对我来说,最简单的方法是使用sapply一次查看每个重量,并将其与其他重量进行比较,寻找小于10的差异。