Assume the following datatable:
DT <- data.table(a=c(1,2,3,4,5,6),b=c(NaN,NaN,NaN,4,5,6),c=c(NaN,3,3,3,NaN,NaN))
How can I replace all NaN
values by NA
, i.e. in each column? Looking for an elegant (short) way to do this, but I am also wondering if it is possible using lapply
, like I tried below.
My approach so far:
DT[,lapply(SD,function(x){x[is.nan(x)] := NA}),.SDcols=c("a","b","c")]
Actual result of my code is:
Error in
:=
(x[is.nan(x)], NA) : Check that is.data.table(DT) == TRUE. Otherwise, := and:=
(...) are defined for use in j, once only and in particular ways. See help(":=").