2

I want to replace NA with other value for specific column in data.table. I tried below link but some error shows up.

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

The codes that I use are

df<-data.table(aa<-(1:4),ba<-c(NA,1,3,4),ca<-c(NA,"2012-01-02","2012-02-02","2012-03-02"))

df[is.na(get(ca)),(ca):="2012-04-01"]

I got the error message: Error in get(c) : object 'NA' not found

But if I use

df[is.na(ca),(ca):="2012-04-01"]

It returns results that I don't want.

Can anyone help me?

Thanks

Community
  • 1
  • 1
kzhang12
  • 211
  • 5
  • 17

1 Answers1

6

If we use the correct column names, it would work, and we don't need get.

df[is.na(ca), ca:= "2012-04-01"]
df
#  aa ba         ca
#1:  1 NA 2012-04-01
#2:  2  1 2012-01-02
#3:  3  3 2012-02-02
#4:  4  4 2012-03-02

Within the data.table call, we use = instead of <-. In addition, as @Frank mentioned, assigning (ca) and ca are different as the former could be a vector of strings that can be used to create names for new columns.

data

df<-data.table(aa=(1:4),ba=c(NA,1,3,4),
     ca=c(NA,"2012-01-02","2012-02-02","2012-03-02"))
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 3
    Maybe worth noting: `(ca) := x` and `ca := x` are very different, since the former (which the OP was doing) actually takes the values of `ca` as names for new columns. – Frank Jan 29 '16 at 18:51
  • 3
    to OP: `get` expects a `character` constant, so `df[is.na(get("c")), ("c") := "2012-04-01"]` would work (though is unnecessarily complicated in this case). `akrun`'s approach is the way to go. – MichaelChirico Jan 29 '16 at 18:57
  • 1
    Thanks. @akrun and @ MichealChirico. I learnt two ways to do that. – kzhang12 Jan 29 '16 at 19:27