Substitute LHS of = in R

六月ゝ 毕业季﹏ 提交于 2019-12-14 03:28:24

问题


I would like to replace the LHS of "=" in a expression in R. In my personal case, I need it to make sure the following creates a variable that does not already exist in the data frame

df %>% mutate(v = mean(w))

I tried eval(substitute()) but the LHS is not substituted

eval(substitute(df %>% mutate(v = mean(w)), list(v = as.name("id"))))
 #similarly in a list
eval(substitute(l <- list(v=1:10),list(v=as.name("id"))))
l
$v
[1]  1  2  3  4  5  6  7  8  9 10

Why can't v substituted throught eval/substitute? What's the best way to work around it?


回答1:


1) eval/parse Create a cmd string, parse it and evaluate it:

f2 <- function(DF, x, env = parent.frame()){
  cmd <- sprintf("mutate(%s, %s = mean(v1))", deparse(substitute(DF)), x)
  eval(parse(text = cmd), env)
}

f2(DF, "v1_name")

giving

  v1 v1_mean
1  1       2
2  2       2
3  3       2
... etc ...

2) eval/as.call Another way is to construct a list, convert it to a call and evaluate it. (This is also the approach that mutate_each_q in dplyr takes.)

f3 <- function(DF, x, env = parent.frame()) {
    L <- list(quote(mutate), .data = substitute(DF), quote(mean(v1)))
    names(L)[3] <- x
    eval(as.call(L), env)
}

f3(DF, "v1_name")

3) do.call We form a list equal to the last two components of the list in the prior solution and then use do.call :

f3 <- function(DF, x, env = parent.frame()) {
    L <- list(.data = substitute(DF), quote(mean(v1)))
    names(L)[2] <- x
    do.call(mutate, L)
}

f3(DF, "v1_name")

Upodate Added additional solutions.



来源:https://stackoverflow.com/questions/25944762/substitute-lhs-of-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!