是否有用于group_by和组合的R代码 [英] is there a R code for group_by and combinations
本文介绍了是否有用于group_by和组合的R代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我好心地寻找可以计算出每组总数中2个组合的R代码.
Am kindly looking for R code that can calculate the combinations with 2, of the total in each group.
library(dplyr)
id<-c(1,1,1,1,2,2,2,2,3,3,3,3)
sex<-c(1,1,1,1,1,1,1,1,1,1,1,1)
ds<-data.frame(id,sex)
out1<-ds %>% group_by(id) %>% summarise(n=n())
我的输出应该是
id<-c(1,2,3)
n<-c(4,4,4)
ncomb2<-c(6,6,6)
谢谢
推荐答案
我不确定下面的代码是否是您想要的
I am not sure if the code below is what you are after
out1 <- ds %>%
group_by(id) %>%
summarise(n = n()) %>%
mutate(ncomb2 = choose(n,2))
如此
> out1
# A tibble: 3 x 3
id n ncomb2
<dbl> <int> <dbl>
1 1 4 6
2 2 4 6
3 3 4 6
这篇关于是否有用于group_by和组合的R代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文