gpt4 book ai didi

r - data.table: “group counter” 对于特定的列组合

转载 作者:行者123 更新时间:2023-12-05 00:18:23 25 4
gpt4 key购买 nike

我想根据一组相同的行在数据框中添加一个计数器列。为此,我使用了包 data.table。在我的例子中,行之间的比较需要从列“z”和(“x”或“y”)的组合中进行。

我测试了:

DF[ , Index := .GRP, by = c("x","y","z") ]

但结果是“z”AND“x”AND“y”的组合。

我怎样才能得到 "z"AND ("x"OR "y") 的组合?

这是一个数据示例:

DF = data.frame(x=c("a","a","a","b","c","d","e","f","f"), y=c(1,3,2,8,8,4,4,6,0), z=c("M","M","M","F","F","M","M","F","F"))
DF <- data.table(DF)

我想要这样的输出:

> DF
x y z Index
1: a 1 M 1
2: a 3 M 1
3: a 2 M 1
4: b 8 F 2
5: c 8 F 2
6: d 4 M 3
7: e 4 M 3
8: f 6 F 4
9: f 0 F 4

最佳答案

如果 z 的值 x 的值发生变化,则新组开始 y 正在改变。

试试这个例子。

require(data.table)

DF <- data.table(x = c("a","a","a","b","c","d","e","f","f"),
y = c(1,3,2,8,8,4,4,6,0),
z=c("M","M","M","F","F","M","M","F","F"))

# The functions to compare if value is not equal with the previous value
is.not.eq.with.lag <- function(x) c(T, tail(x, -1) != head(x, -1))

DF[, x1 := is.not.eq.with.lag(x)]
DF[, y1 := is.not.eq.with.lag(y)]
DF[, z1 := is.not.eq.with.lag(z)]
DF

DF[, Index := cumsum(z1 | (x1 & y1))]
DF

关于r - data.table: “group counter” 对于特定的列组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37865217/

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