繁体   English   中英

R中data.table中的行索引

[英]Row indices in data.table in R

如何控制R中data.table中的行索引?

我想检查一行中的值是否与上一个相匹配:

patient    produkt    output
1          Meg        Initiation
1          Meg        Continue
1          Gem        Switch
2          Pol        Initiation
2          Pol        Continue
2          Pol        Continue

我希望在输出列为输出的地方(如果更容易实现,则可以用数字代替,尽管使initiation=0, continue=1, switch=2 )。

我找不到如何控制data.table中的索引,以下内容不起作用

test[ , switcher2 := identical(produkt, produkt[-1]),by=patient]

任何想法都欢迎。 它必须在data.table中。

这是尝试使用GH开发版本中的新shift函数

我在这里使用了0:2表示法,因为它写起来比较短,但是您可以使用单词代替

test[ , output2 := c(0, (2:1)[(produkt == shift(produkt)) + 1][-1]), by = patient]
#    patient produkt     output output2
# 1:       1     Meg Initiation       0
# 2:       1     Meg   Continue       1
# 3:       1     Gem     Switch       2
# 4:       2     Pol Initiation       0
# 5:       2     Pol   Continue       1
# 6:       2     Pol   Continue       1

我基本上总是从每个组0开始,然后与每个组的先前值进行比较。 如果为TRUE则分配1 如果为FALSE则分配2


如果您想用文字表达,这里是替代版本

test[ ,output3 := c("Initiation", c("Switch", "Continue")[(produkt == shift(produkt)) + 1][-1]), by = patient]

安装说明:

library(devtools)
install_github("Rdatatable/data.table", build_vignettes = FALSE)

这里是使用diff的选项。 我正在使用ifelse将整数值更改为字符。 最后,对于每个组,将第一个元素设置为初始值。

setDT(dx)[,output := {
   xx <- ifelse(c(0,diff(as.integer(factor(produkt))))<0,
                "Switch","Continue")
   xx <- as.character(xx)
   xx[1] <- "Initiation"
   xx
   },
patient]

#   patient produkt     output
# 1:       1     Meg Initiation
# 2:       1     Meg   Continue
# 3:       1     Gem     Switch
# 4:       2     Pol Initiation
# 5:       2     Pol   Continue
# 6:       2     Pol   Continue

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM