gpt4 book ai didi

r - 按列分组的非 NA 记录数

转载 作者:行者123 更新时间:2023-12-04 12:09:25 26 4
gpt4 key购买 nike

我有一个 data.table 看起来像这样:

> dt <- data.table(
group1 = c("a", "a", "a", "b", "b", "b", "b"),
group2 = c("x", "x", "y", "y", "z", "z", "z"),
data1 = c(NA, rep(T, 3), rep(F, 2), "sometimes"),
data2 = c("sometimes", rep(F,3), rep(T,2), NA))

> dt

group1 group2 data1 data2
1: a x NA sometimes
2: a x TRUE FALSE
3: a y TRUE FALSE
4: b y TRUE FALSE
5: b z FALSE TRUE
6: b z FALSE TRUE
7: b z sometimes NA

我的目标是找到每个数据列中非 NA 记录的数量,按 group1 分组。和 group2 .
   group1 group2     data1     data2
1: a x 1 2
3: a y 1 1
4: b y 1 1
5: b z 3 2

我在处理数据集的另一部分时留下了这段代码,该部分没有 NA s 并且是合乎逻辑的:
dt[
,
lapply(.SD, sum),
by = list(group1, group2),
.SDcols = c("data3", "data4")
]

但它不适用于 NA 值或非逻辑值。

最佳答案

dt[, lapply(.SD, function(x) sum(!is.na(x))), by = .(group1, group2)]
# group1 group2 data1 data2
#1: a x 1 2
#2: a y 1 1
#3: b y 1 1
#4: b z 3 2

关于r - 按列分组的非 NA 记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33661645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com