gpt4 book ai didi

R数据表: label the counts of consecutive non-NA values

转载 作者:行者123 更新时间:2023-12-02 08:13:43 25 4
gpt4 key购买 nike

这是我的 data.table,列 column1

library(data.table)

dt = data.table(column1 = c(NA, NA, "A", "A", "A", NA, NA, NA, NA, "B", NA, NA, "1 2", "1 2", NA, NA, "A", "A", "A", "A", "A", NA, NA, NA, NA, ...))

> print(dt)
column1
1: NA
2: NA
3: A
4: A
5: A
6: NA
7: NA
8: NA
9: NA
10: B
11: NA
12: NA
13: 1 2
14: 1 2
15: NA
16: NA
17: A
18: A
19: A
20: A
21: A
22: NA
23: NA
24: NA
25: NA
... ...

column1 中的值是 NA 值或字符。我想按该组中的项目数标记每个连续的非 NA 值组。以下是 dt$labels

的用途
> print(dt)
column1 labels
1: NA 0
2: NA 0
3: A 3
4: A 3
5: A 3
6: NA 0
7: NA 0
8: NA 0
9: NA 0
10: B 1
11: NA 0
12: NA 0
13: 1 2 2
14: 1 2 2
15: NA 0
16: NA 0
17: A 5
18: A 5
19: A 5
20: A 5
21: A 5
22: NA 0
23: NA 0
24: NA 0
25: NA 0
... ... ...

有 3 个连续的 A、1 个“B”、2 个“1 2”和 5 个“A”。

使用 rle()

x <- rle(dt$column1) 

将给出每个唯一值的长度

 Run Length Encoding                                                                                                                                                                                                                                                                        
lengths: int [1:18] 1 1 3 1 1 1 1 1 1 1 ...
values : chr [1:18] NA NA "A" NA NA NA NA "B" NA NA "1 2" ...

但我不确定如何将这些长度映射到 data.table 列 labels

最佳答案

我们可以使用 data.table 中的 rleid 创建一个分组变量,然后将逻辑向量乘以 .N 并赋值 (:=) 输出到'labels'

dt[, labels := .N*!is.na(column1), rleid(is.na(column1))]
dt
# column1 labels
# 1: NA 0
# 2: NA 0
# 3: A 3
# 4: A 3
# 5: A 3
# 6: NA 0
# 7: NA 0
# 8: NA 0
# 9: NA 0
#10: B 1
#11: NA 0
#12: NA 0
#13: 1 2 2
#14: 1 2 2
#15: NA 0
#16: NA 0
#17: A 5
#18: A 5
#19: A 5
#20: A 5
#21: A 5
#22: NA 0
#23: NA 0
#24: NA 0
#25: NA 0

数据

dt <- data.table(column1 = c(NA, NA, "A", "A", "A", NA, NA, NA, NA, "B", 
NA, NA, "1 2", "1 2", NA, NA, "A", "A", "A", "A", "A", NA, NA, NA, NA))

关于R数据表: label the counts of consecutive non-NA values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43855095/

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