[英]Replace values in several data.table's columns that meet a condition
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))
I wish to replace values of 'cat' and 'fox' that are equal to '0' or '2' with '-99'我希望用“-99”替换等于“0”或“2”的“猫”和“狐狸”的值
I can do it one at a time but how to do them both?我可以一次做一个,但如何同时做呢?
dat[fox == 0 | fox == 2, fox := -99]
Another approach with data.table is using a for(...) set(...)
-approach, which is in this case both fast and memory efficient: data.table的另一种方法是使用for(...) set(...)
-方法,在这种情况下既快速又高效 memory:
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]
which gives:这使:
> 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))
Here's a not-so-elegant way of doing this:这是一种不太优雅的方法:
> 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
I'm calling (c('cat', 'fox'))
explicitly, but you could save them as mycols
and assign using :=
operator: data[, mycols:=...]
我正在显式调用(c('cat', 'fox'))
,但您可以将它们保存为mycols
并使用:=
operator: data[, mycols:=...]
Similarly, I'm passing a list explicitly based on the conditions - this could be better done using a function instead.同样,我根据条件明确传递了一个列表——这可以更好地使用 function 来代替。
If I understand, this would work as well:如果我理解,这也可以:
cols = c("cat", "fox")
data[, (cols) := lapply(.SD, function (x) fifelse(x %in% c(0, 2), -99, x)), .SDcols = cols]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.