我试图替换data.table中重复行的值。我们说你有
A <- c(1,2,3,4,4,6,4)
B <- c("a","b","c","d","e","f","g")
C <- c(10,11,23,8,8,1,3)
dt <- data.table(A,B,C)
我想:dt[duplicated(dt,dt[,c(1,3)]),][,2] <- 0
获取
>dt
A B C
1: 1 a 10
2: 2 b 11
3: 3 c 23
4: 4 d 8
5: 4 0 8
6: 6 f 1
7: 4 g 3
答案 0 :(得分:1)
你可以做到
> A <- c(1,2,3,4,4,6,4)
> B <- c("a","b","c","d","e","f","g")
> dt <- data.table(A,B,C, stringsAsFactors = FALSE)
> C <- c(10,11,23,8,8,1,3)
> dt[dt[, j = duplicated(.SD), .SDcols = c("A", "C")], B := "0"]
> dt
A B C
1: 1 a 10
2: 2 b 11
3: 3 c 23
4: 4 d 8
5: 4 0 8
6: 6 f 1
7: 4 g 3
......但现在看到大卫的解决方案更加简洁......