How would you replace all values in a data.table given a condition?
For example
ppp <- data.table(A=1:6,B=6:1,C=1:6,D=3:8)
A B C D
1 6 1 3
2 5 2 4
3 4 3 5
4 3 4 6
5 2 5 7
6 1 6 8
I want to replace all "6" by NA
A B C D
1 NA 1 3
2 5 2 4
3 4 3 5
4 3 4 NA
5 2 5 7
NA 1 6 8
I've tried something like
ppp[,ifelse(.SD==6,NA,.SD)]
but it doesn't work, it produces a much wider table.
Even easier:
ppp[ppp == 6] <- NA
 ppp
    A  B  C  D
1:  1 NA  1  3
2:  2  5  2  4
3:  3  4  3  5
4:  4  3  4 NA
5:  5  2  5  7
6: NA  1 NA  8
Importantly, this doesn't change its class:
is.data.table(ppp)
[1] TRUE
                        A native data.table way to do this would be:
for(col in names(ppp)) set(ppp, i=which(ppp[[col]]==6), j=col, value=NA)
# Test
> ppp
    A  B  C  D
1:  1 NA  1  3
2:  2  5  2  4
3:  3  4  3  5
4:  4  3  4 NA
5:  5  2  5  7
6: NA  1 NA  8
This approach - while perhaps more verbose - is nevertheless going to be significantly faster than ppp[ppp == 6] <- NA, because it avoids the copying of all columns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With