Questions tagged «summarization»

1
以可读的方式获取不重复的dplyr计数
我是使用dplyr的新手,我需要计算一组中的不同值。这是一个表格示例: data=data.frame(aa=c(1,2,3,4,NA), bb=c('a', 'b', 'a', 'c', 'c')) 我知道我可以做类似的事情: by_bb<-group_by(data, bb, add = TRUE) summarise(by_bb, mean(aa, na.rm=TRUE), max(aa), sum(!is.na(aa)), length(aa)) 但是,如果我要计算独特元素的数量? 我可以: > summarise(by_bb,length(unique(unlist(aa)))) bb length(unique(unlist(aa))) 1 a 2 2 b 1 3 c 2 如果我想排除NA,我可以这样做: > summarise(by_bb,length(unique(unlist(aa[!is.na(aa)])))) bb length(unique(unlist(aa[!is.na(aa)]))) 1 a 2 2 b 1 3 c 1 但这对我来说有点难以理解。有没有更好的方法来进行这种总结?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.