数据集包含三个变量:id,sex和grade(factor).
mydata <- data.frame(id=c(1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4), sex=c(1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1),
grade=c("a","b","c","d","e", "x","y","y","x", "q","q","q","q", "a", "a", "a", NA, "b"))
Run Code Online (Sandbox Code Playgroud)
对于每个ID,我需要查看我们有多少个唯一等级,然后创建一个新列(调用N)来记录等级频率.例如,对于ID = 1,我们有五个"等级"的唯一值,因此N = 4; 对于ID = 2,我们有两个"等级"的唯一值,所以N = 2; 对于ID = 4,我们有两个"等级"的唯一值(忽略NA),因此N = 2.
最终的数据集是
mydata <- data.frame(id=c(1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4), sex=c(1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1),
grade=c("a","b","c","d","e", "x","y","y","x", "q","q","q","q", "a", "a", "a", NA, "b"))
mydata$N <- c(5,5,5,5,5,2,2,2,2,1,1,1,1,2,2,2,2,2)
Run Code Online (Sandbox Code Playgroud)
Jaa*_*aap 11
新答案:
data.table的uniqueN函数有一个参数,我们可以使用如下:na.rm
library(data.table)
setDT(mydata)[, n := uniqueN(grade, na.rm = TRUE), by = id]
Run Code Online (Sandbox Code Playgroud)
这使:
Run Code Online (Sandbox Code Playgroud)> mydata id sex grade n 1: 1 1 a 5 2: 1 1 b 5 3: 1 1 c 5 4: 1 1 d 5 5: 1 1 e 5 6: 2 0 x 2 7: 2 0 y 2 8: 2 0 y 2 9: 2 0 x 2 10: 3 0 q 1 11: 3 0 q 1 12: 3 0 q 1 13: 3 0 q 1 14: 4 1 a 2 15: 4 1 a 2 16: 4 1 a 2 17: 4 1 NA 2 18: 4 1 b 2
老答案:
使用data.table,您可以按如下方式执行此操作:
library(data.table)
setDT(mydata)[, n := uniqueN(grade[!is.na(grade)]), by = id]
Run Code Online (Sandbox Code Playgroud)
要么:
setDT(mydata)[, n := uniqueN(na.omit(grade)), by = id]
Run Code Online (Sandbox Code Playgroud)
你可以使用这个包data.table:
library(data.table)
setDT(mydata)
#I have removed NA's, up to you how to count them
mydata[,N_u:=length(unique(grade[!is.na(grade)])),by=id]
Run Code Online (Sandbox Code Playgroud)
非常简短,可读且快速.它也可以在base-R中完成:
#lapply(split(grade,id),...: splits data into subsets by id
#unlist: creates one vector out of multiple vectors
#rep: makes sure each ID is repeated enough times
mydata$N <- unlist(lapply(split(mydata$grade,mydata$id),function(x){
rep(length(unique(x[!is.na(x)])),length(x))
}
))
Run Code Online (Sandbox Code Playgroud)
因为有关于什么更快的讨论,让我们做一些基准测试.
给定数据集:
> test1
Unit: milliseconds
expr min lq mean median uq max neval cld
length_unique 3.043186 3.161732 3.422327 3.286436 3.477854 10.627030 100 b
uniqueN 2.481761 2.615190 2.763192 2.738354 2.872809 3.985393 100 a
Run Code Online (Sandbox Code Playgroud)
更大的数据集:(10000个观察值,1000个id)
> test2
Unit: milliseconds
expr min lq mean median uq max neval cld
length_unique 11.84123 24.47122 37.09234 30.34923 47.55632 97.63648 100 a
uniqueN 25.83680 50.70009 73.78757 62.33655 97.33934 210.97743 100 b
Run Code Online (Sandbox Code Playgroud)
使用dplyr::n_distinct及其na.rm-argument的dplyr选项:
library(dplyr)
mydata %>% group_by(id) %>% mutate(N = n_distinct(grade, na.rm = TRUE))
#Source: local data frame [18 x 4]
#Groups: id [4]
#
# id sex grade N
# (dbl) (dbl) (fctr) (int)
#1 1 1 a 5
#2 1 1 b 5
#3 1 1 c 5
#4 1 1 d 5
#5 1 1 e 5
#6 2 0 x 2
#7 2 0 y 2
#8 2 0 y 2
#9 2 0 x 2
#10 3 0 q 1
#11 3 0 q 1
#12 3 0 q 1
#13 3 0 q 1
#14 4 1 a 2
#15 4 1 a 2
#16 4 1 a 2
#17 4 1 NA 2
#18 4 1 b 2
Run Code Online (Sandbox Code Playgroud)
看起来我们有几个投票data.table,但您也可以使用基本R函数ave():
mydata$N <- ave(as.character(mydata$grade),mydata$id,
FUN = function(x) length(unique(x[!is.na(x)])))
Run Code Online (Sandbox Code Playgroud)