计算 r 中的唯一因子 [英] counting unique factors in r
本文介绍了计算 r 中的唯一因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道在记录的每个出生日期分娩的独特水坝的数量.我的数据框类似于这个:
I would like to know the number of unique dams which gave birth on each of the birth dates recorded. My data frame is similar to this one:
dam <- c("2A11","2A11","2A12","2A12","2A12","4D23","4D23","1X23")
bdate <- c("2009-10-01","2009-10-01","2009-10-01","2009-10-01",
"2009-10-01","2009-10-03","2009-10-03","2009-10-03")
mydf <- data.frame(dam,bdate)
mydf
# dam bdate
# 1 2A11 2009-10-01
# 2 2A11 2009-10-01
# 3 2A12 2009-10-01
# 4 2A12 2009-10-01
# 5 2A12 2009-10-01
# 6 4D23 2009-10-03
# 7 4D23 2009-10-03
# 8 1X23 2009-10-03
我使用了 aggregate(dam ~ bdate, data=mydf, FUN=length)
但它计算了在特定日期分娩的所有水坝
I used aggregate(dam ~ bdate, data=mydf, FUN=length)
but it counts all the dams that gave birth on a particular date
bdate dam
1 2009-10-01 5
2 2009-10-03 3
相反,我需要这样的东西:
Instead, I need to have something like this:
mydf2
bdate dam
1 2009-10-01 2
2 2009-10-03 2
非常感谢您的帮助!
推荐答案
关于:
aggregate(dam ~ bdate, data=mydf, FUN=function(x) length(unique(x)))
这篇关于计算 r 中的唯一因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文