how to create a quoted expression from strings

后端 未结 3 933
遥遥无期
遥遥无期 2021-01-31 12:27

Given a vector of strings, I would like to create an expression without the quotation marks.

# eg, I would like to go from 
c(\"string1\", \"string2\")

# to...         


        
3条回答
  •  星月不相逢
    2021-01-31 12:35

    I tend to use as.quoted from the plyr package

     outputString <- sprintf('list(%s)', paste(input, collapse = ', ')) 
    
    
     library(plyr)
      output <- as.quoted(outputString)[[1]]
    
      mydt[, eval(output)]
       string1 string2
    1:       A       a
    2:       B       b
    3:       C       c
    

    However if it is simply column selection, you can pass the string and use .. (which, like in the Unix terminal, means to "look up one level")

    mydt[ , ..input]
       string1 string2
    1:       A       a
    2:       B       b
    3:       C       c
    

提交回复
热议问题