R:模仿函数中的函数
我已经为此工作了几个小时,我几乎被卡住了。我想要一个类似于R:模仿函数中的函数,r,function,R,Function,我已经为此工作了几个小时,我几乎被卡住了。我想要一个类似于with函数的函数,但允许我使用data.frame中的其他变量或外部值在data.frame中生成新变量 MYDATA <- data.frame(a1=rnorm(5), a2=rnorm(5)) dg <- function(svar, exp, tempdata="MYDATA") { tempdata <- get(tempdata) tempdata[svar] <- with(tempdat
with
函数的函数,但允许我使用data.frame
中的其他变量或外部值在data.frame
中生成新变量
MYDATA <- data.frame(a1=rnorm(5), a2=rnorm(5))
dg <- function(svar, exp, tempdata="MYDATA") {
tempdata <- get(tempdata)
tempdata[svar] <- with(tempdata, eval(substitute(exp)))
return(tempdata)
}
MYDATA
# a1 a2
#1 0.07113 0.84302
#2 -0.46902 -1.12064
#3 1.01703 0.62591
#4 1.13271 0.24405
#5 -0.24867 -0.02474
谢谢你的考虑!
弗朗西斯职能机构的第二行应该是:
tempdata[svar] <- eval(substitute(exp), tempdata)
tempdata[svar]您是否在
中查找?非常感谢!令人惊叹的!我没有意识到我离得这么近!
tempdata[svar] <- eval(substitute(exp), tempdata)