How to delete a row by reference in data.table?

前端 未结 6 916
南方客
南方客 2020-11-22 16:07

My question is related to assignment by reference versus copying in data.table. I want to know if one can delete rows by reference, similar to

         


        
6条回答
  •  一个人的身影
    2020-11-22 16:56

    The topic is still interesting many people (me included).

    What about that? I used assign to replace the glovalenv and the code described previously. It would be better to capture the original environment but at least in globalenv it is memory efficient and acts like a change by ref.

    delete <- function(DT, del.idxs) 
    { 
      varname = deparse(substitute(DT))
    
      keep.idxs <- setdiff(DT[, .I], del.idxs)
      cols = names(DT);
      DT.subset <- data.table(DT[[1]][keep.idxs])
      setnames(DT.subset, cols[1])
    
      for (col in cols[2:length(cols)]) 
      {
        DT.subset[, (col) := DT[[col]][keep.idxs]]
        DT[, (col) := NULL];  # delete
      }
    
      assign(varname, DT.subset, envir = globalenv())
      return(invisible())
    }
    
    DT = data.table(x = rep(c("a", "b", "c"), each = 3), y = c(1, 3, 6), v = 1:9)
    delete(DT, 3)
    

提交回复
热议问题