Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all values in a data.table given a condition

Tags:

r

data.table

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.

like image 662
skan Avatar asked Jul 06 '16 14:07

skan


2 Answers

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
like image 135
lmo Avatar answered Oct 25 '22 16:10

lmo


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.

like image 44
mtoto Avatar answered Oct 25 '22 16:10

mtoto