Referring to data.table columns by names saved in variables

前端 未结 4 1049
借酒劲吻你
借酒劲吻你 2020-11-29 21:21

data.table is a fantastic R package and I am using it in a library I am developing. So far all is going very well, except for one complication. It seems to be m

4条回答
  •  温柔的废话
    2020-11-29 22:09

    Maybe you know about this solution already?

    DT[[colname]]
    

    This is inspired by @eddi's solution in the comments below, using the OP's example:

    set.seed(1)
    x = data.table(a = 1:10, b=rnorm(10))
    colstr="b"
    col <- eval(parse(text=paste("quote(",colstr,")",sep="")))
    x[eval(col)<0]
    x[eval(col)<0,c(colstr):=-100]
    

提交回复
热议问题