jnam27 - 2 months ago 9
R Question

How to replace NA values in a table *for selected columns*? data.frame, data.table

There are a lot of posts about replacing NA values. I am aware that one could replace NAs in the following table/frame with the following:

``````x[is.na(x)]<-0
``````

But, what if I want to restrict it to only certain columns? Let's me show you an example.

``````set.seed(1234)
x <- data.frame(a=sample(c(1,2,NA), 10, replace=T),
b=sample(c(1,2,NA), 10, replace=T),
c=sample(c(1:5,NA), 10, replace=T))
``````

Which gives:

``````    a  b  c
1   1 NA  2
2   2  2  2
3   2  1  1
4   2 NA  1
5  NA  1  2
6   2 NA  5
7   1  1  4
8   1  1 NA
9   2  1  5
10  2  1  1
``````

Ok, so I only want to restrict the replacement to columns 'a' and 'b'. My attempt was:

``````x[is.na(x), 1:2]<-0
``````

and:

``````x[is.na(x[1:2])]<-0
``````

Which does not work.

My data.table attempt, where
`y<-data.table(x)`
, was obviously never going to work:

``````y[is.na(y[,list(a,b)]), ]
``````

I want to pass columns inside the is.na argument but that obviously wouldn't work.

I would like to do this in a data.frame and a data.table. My end goal is to recode the 1:2 to 0:1 in 'a' and 'b' while keeping 'c' the way it is, since it is not a logical variable. I have a bunch of columns so I don't want to do it one by one. And, I'd just like to know how to do this.

Do you have any suggestions?

You can do:

``````x[, 1:2][is.na(x[, 1:2])] <- 0
``````

or better (IMHO), use the variable names:

``````x[c("a", "b")][is.na(x[c("a", "b")])] <- 0
``````

In both cases, `1:2` or `c("a", "b")` can be replaced by a pre-defined vector.