0

I have a list of variables with missing values. I would like for those selected columns to replace that missing value by 0 (or a blank space or whatever).

How can I achieve this with data.table ? My attempts with nafill failed because my variables aren't numeric.

start <- data.table::data.table(DAS1 = c("2",NA,"x","2","1","2","1"),
                       DAS2 = c("x","y","2","2", NA,"1","2"),
                       DAS3 = c("1","1","y", NA, NA,"y", NA))

end <- data.table::data.table(DAS1 = c("2","0","x","2","1","2","1"),
                       DAS2 = c("x","y","2","2", "0","1","2"),
                       DAS3 = c("1","1","y", "0", "0","y", "0"))

1 Answers1

0

As a data.table is also a data.frame, this will work :

start[is.na(start)]<-0

all.equal(start,end)
#[1] TRUE
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • It doesn't : `Error in as.Date.numeric(value) : 'origin' must be supplied` – Kévin Legueult Mar 04 '22 at 14:49
  • It's strange that `DT[A]` doesn't work if `A` is a two-column matrix and `DT` a data.table, while `DT[A]<-value` works. Can't guess whether this was intended or not by the data.table guys. – nicola Mar 04 '22 at 14:49
  • 1
    @KévinLegueult, could you provide dates in your example? – Waldi Mar 04 '22 at 14:51
  • 1
    Generally, it is a better idea to use native update-by-reference methods when working with `data.table`. The difference in performance only becomes important with larger tables, but is often a key benefit of using the package. See linked duplicates for native data.table solutions. – dww Mar 04 '22 at 15:00