# 数据集中哪些变量在ID中是常量

``````dt[ , count := .N, by = id][count > 1, ]
``````

（也使用`head``tail`），但是有27000次（总计）重复观察-无法通过检查来确定哪些变量在变化。

``````dt[ , count := .N, by = id]
for (var in setdiff(names(dt), c("id", "count"))){
if (nrow(dt[ , list(.N, count), by = c("id", var)][N < count, ]) > 0){
print(var)
}
}
``````

# 编辑

``````set.seed(2304)
DT <- data.table(a = rep(sample(5), each = 3),
b = sample(15),
c = rep(LETTERS[1:5], each = 3),
d = rnorm(15),
e = rep(6:10, each = 3),
f = 15:1,
grp = paste0("Group ", rep(1:5, each = 3)))
``````

``````c("a", "c", "e")
``````

``````data.table(mtcars)[, lapply(.SD, function(x) length(unique(x))), by=carb]
``````

``````   carb mpg cyl disp hp drat wt qsec vs am gear
1:    4   8   2    8  7    8  9   10  2  2    3
2:    1   7   2    7  6    6  7    7  1  2    2
3:    2   9   2   10  8    9 10   10  2  2    3
4:    3   3   1    1  1    1  3    3  1  1    1
5:    6   1   1    1  1    1  1    1  1  1    1
6:    8   1   1    1  1    1  1    1  1  1    1
``````

``````data.table(mtcars)[,lapply(.SD,uniqueN),by=carb
][,!"carb"][,lapply(.SD,table)]

mpg cyl disp hp drat wt qsec vs am gear
1:   2   3    3  3    3  2    2  4  3    3
2:   1   3    1  1    1  1    1  2  3    1
3:   1   3    1  1    1  1    1  4  3    2
4:   1   3    1  1    1  1    2  2  3    3
5:   1   3    3  3    3  1    2  4  3    1
``````

``````data.table(mtcars)[,lapply(.SD,uniqueN),by=disp
][,!"disp"][,lapply(.SD,table)]

mpg cyl hp drat wt qsec vs am gear carb
1:  24  27 26   26 24   23 27 27   27   26
2:   2  27  1    1  2    3 27 27   27    1
3:   1  27 26   26  1    1 27 27   27   26
``````

0条评论