gpt4 book ai didi

r - 如何对由其他列定义的组内的列中的值进行索引?

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

我有一个数据框:

dput(df)
structure(list(ID = c("A1", "A1", "A1", "A1", "A1", "A1", "B2",
"B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2"), operation = c("open",
"open", "close", "", "open", "close", "", "open", "open", "open",
"close", "upload", "open", "close", "open", "close")), class = "data.frame", row.names = c(NA,
-16L))
ID      operation
A1 open
A1 open
A1 close
A1
A1 open
A1 close
B2
B2 open
B2 open
B2 open
B2 close
B2 upload
B2 open
B2 close
B2 open
B2 close
我想在列操作中为每个“打开”和“关闭”包添加索引。所以对于 open 和 close 之间的每一行都必须具有相同的索引。所以想要的结果是:
ID      operation    index
A1 open 1
A1 open 1
A1 close 1
A1
A1 open 2
A1 close 2
B2
B2 open 3
B2 open 3
B2 open 3
B2 close 3
B2 upload
B2 open 4
B2 close 4
B2 open 5
B2 close 5
我是这样做的:
rank <- df$operation == "close" & !is.na(df$operation)
df$index <- cumsum(c(1, rank[-length(rank)]))
df$index[!df$operation %in% c("open", "close")] <- NA
现在我想在每个 ID 中建立这个索引。所以对于每个 ID 索引从头开始。所以想要的结果是:
ID      operation    index
A1 open 1
A1 open 1
A1 close 1
A1
A1 open 2
A1 close 2
B2
B2 open 1
B2 open 1
B2 open 1
B2 close 1
B2 upload
B2 open 2
B2 close 2
B2 open 3
B2 close 3
我怎么能那样做?

最佳答案

这是使用 data.table 的一种方法:

library(data.table)

setDT(df)
df[!is.na(index), index := rleid(index), by = .(ID)]
df
# ID operation index
# 1: A1 open 1
# 2: A1 open 1
# 3: A1 close 1
# 4: A1 NA
# 5: A1 open 2
# 6: A1 close 2
# 7: B2 NA
# 8: B2 open 1
# 9: B2 open 1
# 10: B2 open 1
# 11: B2 close 1
# 12: B2 upload NA
# 13: B2 open 2
# 14: B2 close 2
# 15: B2 open 3
# 16: B2 close 3

有名为 dtdf 的内置 R 函数。虽然 R 可以区分变量和函数,但最好使用其他名称。

关于r - 如何对由其他列定义的组内的列中的值进行索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65791730/

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