Replacing all missing values in R data.table with a value

前端 未结 4 1646
清歌不尽
清歌不尽 2020-12-04 14:05

If you have an R data.table that has missing values, how do you replace all of them with say, the value 0? E.g.

aa = data.table(V1=1:10,V2=c(1,2,2,3,3,3,4,4,         


        
4条回答
  •  天命终不由人
    2020-12-04 14:29

    Nothing unusual here:

    tt[is.na(tt)] = 0
    

    ..will work.

    This is somewhat confusing however given that:

    tt[is.na(tt)]
    

    ...currently returns:

    Error in [.data.table(tt, is.na(tt)) : i is invalid type (matrix). Perhaps in future a 2 column matrix could return a list of elements of DT (in the spirit of A[B] in FAQ 2.14). Please let datatable-help know if you'd like this, or add your comments to FR #1611.

提交回复
热议问题