替换满足条件的几个data.table列中的值

时间:2020-04-25 13:30:56

标签: r data.table

library(data.table)
data = data.table("cat" = c(0,5,NA,0,0,0),
                  "horse" = c(0,4,2,1,1,3),
                  "fox" = c(2,2,NA,NA,7,0))

我希望将'cat'和'fox'等于'0'或'2'的值替换为'-99'

我一次可以做一次,但是两者都做吗?

dat[fox == 0 | fox == 2, fox := -99]

3 个答案:

答案 0 :(得分:3)

使用的另一种方法是使用for(...) set(...)方法,这种方法既快速又节省内存:

cols <- c('fox', 'cat')

# option 1
for (j in cols) d[get(j) %in% c(0, 2), (j) := -99]

# option 2 (thx to @Cole for highlighting)
for (j in cols) set(d, which(d[[j]] %in% c(0, 2)), j, value = -99)

# option 3 (thx to @Frank for highlighting)
for (j in cols) d[.(c(0,2)), on = j, (j) := -99]

给出:

> d
   cat horse fox
1: -99     0 -99
2:   5     4 -99
3:  NA     2  NA
4: -99     1  NA
5: -99     1   7
6: -99     3 -99

d  <- data.table("cat"   = c(0,5,NA,0,0,0),
                 "horse" = c(0,4,2,1,1,3),
                 "fox"   = c(2,2,NA,NA,7,0))

答案 1 :(得分:1)

这是一种不太优雅的方法:

> data
   cat horse fox
1:   0     0   2
2:   5     4   2
3:  NA     2  NA
4:   0     1  NA
5:   0     1   7
6:   0     3   0

> data[, c('fox', 'cat') := list(ifelse(cat %in% c(0,2) | fox %in% c(0,2), 99, cat ), ifelse(cat %in% c(0,2) | fox %in% c(0,2), 99, cat ))]
> data
   cat horse fox
1:  99     0  99
2:  99     4  99
3:  NA     2  NA
4:  99     1  99
5:  99     1  99
6:  99     3  99

我正在明确呼叫(c('cat', 'fox')),但是您可以将它们另存为mycols,并使用:=运算符进行分配:data[, mycols := ...]

类似地,我根据条件显式传递了一个列表-最好使用函数代替。

答案 2 :(得分:1)

如果我了解,这也可以:

cols = c("cat", "fox")
data[, (cols) := lapply(.SD, function (x) fifelse(x %in% c(0, 2), -99, x)), .SDcols = cols]