R data.table multi column recode/sub-assign [duplicate]

大兔子大兔子 提交于 2019-12-01 20:02:38

You could use set for replacing values in multiple columns. Based on the ?set, it is fast as the overhead of [.data.table is avoided. We use a for loop to loop over the columns and replace the values that were indexed by the 'i' and 'j' with 'NA'

 for(j in seq_along(DT)) {
      set(DT, i=which(DT[[j]] %in% c(1,7)), j=j, value=NA)
  }

EDIT: Included @David Arenburg's comments.

data

set.seed(24)
DT<-data.table(V1=sample(10), V2= sample(10), V3= sample(10))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!