R data.table二进制值,按条件分组的最后一行

时间:2019-05-10 22:03:02

标签: r data.table

我有这样的数据:

library(data.table)
id <- c("1232","1232","1232","4211","4211","4211")
conversion <- c(0,0,0,1,1,1)
DT <- data.table(id, conversion)

id   date         conversion
1232 2018-01-01   0
1232 2018-01-03   0
1232 2018-01-04   0
4211 2018-04-01   1
4211 2018-04-04   1
4211 2018-04-06   1

我只想基于id行为每个组的最后一行创建一个二进制值。仅当该组的转换为1时,二进制文件才会为1。

id   date         conversion  lastconv
1232 2018-01-01   0           0
1232 2018-01-03   0           0 
1232 2018-01-04   0           0
4211 2018-04-01   1           0
4211 2018-04-04   1           0
4211 2018-04-06   1           1

我尝试在data.table中使用一些带有“ mult”参数的示例,但只返回了错误。

DT[unique(id), lastconv := 1, mult = "last"]

5 个答案:

答案 0 :(得分:5)

对于每个id,请检查行号是否为组中的最后一个行号,并且'conversion'是否为1。将逻辑结果转换为整数。

DT[ , lastconv := as.integer(.I == .I[.N] & conversion == 1), by = id]

答案 1 :(得分:5)

修改OP的代码以加入每个组的最后一行:

DT[, v := 0]
DT[.(DT[conversion == 1, unique(id)]), on=.(id), mult="last", v := 1]

     id conversion v
1: 1232          0 0
2: 1232          0 0
3: 1232          0 0
4: 4211          1 0
5: 4211          1 0
6: 4211          1 1

唯一的不同在于,它根据所需条件选择要编辑的id

答案 2 :(得分:4)

过滤每组的最后一行,并将lastconv设置为等于conversion

DT[DT[, .I[.N], by=id]$V1, lastconv := conversion]

然后将NA替换为0

DT[is.na(lastconv), lastconv := 0L]

结果

DT
#     id conversion lastconv
#1: 1232          0        0
#2: 1232          0        0
#3: 1232          0        0
#4: 4211          1        0
#5: 4211          1        0
#6: 4211          1        1

如果安装了data.table v1.12.3,我们还可以在第二步中使用新功能setnafill替换NA

DT[DT[, .I[.N], by=id]$V1, lastconv := conversion]
setnafill(DT, cols = "lastconv", fill = 0L)

答案 3 :(得分:4)

参考时间:

library(data.table)
#data.table 1.12.3 IN DEVELOPMENT built 2019-05-12 17:04:48 UTC; root using 4 threads (see ?getDTthreads).  Latest news: r-datatable.com
set.seed(0L)
nid <- 3e6L
DT <- data.table(id=rep(1L:nid, each=3L))[,
    conversion := sample(c(0L,1L), 1L, replace=TRUE), by=.(id)]
DT0 <- copy(DT)
DT1 <- copy(DT)
DT2 <- copy(DT)
DT3 <- copy(DT)

mtd0 <- function() {
    DT0[DT0[, .I[.N], by=id]$V1, lastconv := conversion]
    DT0[is.na(lastconv), lastconv := 0L]
}

mtd1 <- function() {
    DT1[DT1[, .I[.N], by=id]$V1, lastconv := conversion]
    setnafill(DT1, cols = "lastconv", fill = 0L)
}

mtd2 <- function() {
    DT2[, v := 0]
    DT2[.(DT2[conversion == 1, unique(id)]), on=.(id), mult="last", v := 1]

    #or also
    #DT2[, v := 0L][
    #    DT2[,.(cv=last(conversion)), id], on=.(id), mult="last", v := cv]
}

mtd3 <- function() {
    DT3[ , lastconv := as.integer(.I == .I[.N] & conversion == 1), by = id]
}

library(microbenchmark)
microbenchmark(mtd0(), mtd1(), mtd2(), mtd3(), times=1L)

时间:

Unit: milliseconds
   expr       min        lq      mean    median        uq       max neval cld
 mtd0() 1363.1783 1416.1867 1468.9256 1469.1952 1521.7992 1574.4033     3  b 
 mtd1() 1349.5333 1365.4653 1378.9350 1381.3974 1393.6358 1405.8743     3  b 
 mtd2()  511.5615  515.4728  552.9133  519.3841  573.5892  627.7944     3 a  
 mtd3() 3966.8867 4009.1128 4048.9607 4051.3389 4089.9977 4128.6564     3   c

答案 4 :(得分:3)

您是否尝试过以下方法?

library(tidyverse)

final_conversion_dat <- DT %>% 
  group_by(id) %>% 
  mutate(date = as.Date(date),
         final_conversion = ifelse(date == max(date, na.rm = T) & conversion == 1, 1, 0))