How to convert a string in a function into an object?

前端 未结 5 847
你的背包
你的背包 2020-12-14 17:55

I would like to convert a string that I pass in a function into an object (or column name).

I know that this works:

df <- data.frame(A = 1:10, B =         


        
5条回答
  •  执念已碎
    2020-12-14 18:56

    The trick is to use parse. For instance:

    > x <- "A"
    > eval(parse(text=paste("df$", x, sep = "")))
     [1]  1  2  3  4  5  6  7  8  9 10
    

    See also this Q/A: Evaluate expression given as a string

提交回复
热议问题