NA in data.table

前端 未结 3 1564
遇见更好的自我
遇见更好的自我 2021-02-20 15:24

I have a data.table that contains some groups. I operate on each group and some groups return numbers, others return NA. For some reason data.ta

3条回答
  •  故里飘歌
    2021-02-20 15:57

    If you want to assign NAs to many variables, you could use the approach suggested here:

    v_1  <- c(0,0,1,2,3,4,4,99)
    v_2  <- c(1,2,2,2,3,99,1,0)
    dat  <-  data.table(v_1,v_2)
    
    for(n in 1:2) {
      chari <-  paste0(sprintf('v_%s' ,n), ' %in% c(0,99)')
      charj <- sprintf('v_%s := NA_integer_', n)
      dat[eval(parse(text=chari)), eval(parse(text=charj))]
    }
    

提交回复
热议问题