r - R:如何将字符串参数转换为变量?
问题描述
所以我有以下数据。我想将列名(字符串)作为参数传递到my_func
函数中,字符串变量将转换为下面选项(1)中显示的变量。我知道我可以做选项(2),但我想知道如何使用选项(1)来做。
最后将新列名作为参数传递,并将其作为新列分配给 xts。
df_xts <- data.frame(date = structure(c(1167667200, 1167753600, 1167840000, 1167926400, 1168012800,
1168099200, 1168185600, 1168272000, 1168358400, 1168444800, 1168531200,
1168617600, 1168704000, 1168790400, 1168876800, 1168963200, 1169049600,
1169136000, 1169222400, 1169308800, 1169395200, 1169481600, 1169568000,
1169654400, 1169740800, 1169827200, 1169913600, 1.17e+09, 1170086400
), tzone = "", tclass = c("POSIXct", "POSIXt"), class = c("POSIXct", "POSIXt")),x=1:29,y1=rnorm(29),y2=rnorm(29,2,2),y3=rnorm(29,3,3),y4=rnorm(29,4,4))
df_xts <- as.xts(df_xts[,c(2:5)],order.by=df_xts$date)
my_func <- function(x,y,y_new,df){
# option (1) how do I convert string variables in the arguments to variables such that i can plug into the formula ?
lr <- lm(y ~ ns(x,df=5),data=df)
# option (2) I know I can do it this way buut this is not what i want. I want to know how to do in the way above?
lr <- lm(df[,c(y)] ~ ns(df[,c(x)],df=5))
# finally assign new column to xts object
df$y_new <- predict(lr, newdata=df$x,se=T)
return(df)
}
my_func(x='x',y='y1',y_new = 'y1_new',df=df_xts)
最终,我想将 lapply
上面的功能跨越c("y1","y2","y3","y4")
.
解决方案
您可以使用ensym
from rlang
package ,它允许您将参数作为函数传递string
或传递symbol
给函数,然后在uationsubstitute
之前传递它们:eval
my_func <- function(x,y,y_new,df){
x <- rlang::ensym(x)
y <- rlang::ensym(y)
y_new <- rlang::ensym(y_new)
lr <- eval(substitute(lm(y ~ splines::ns(x,df=5),data=df),list(x=x,y=y)))
# finally assign new column to xts object
eval(substitute(df$y_new <- predict(lr),list(y_new = y_new)))
return(df)
}
> my_func(x='x',y='y1',y_new = 'y1_new',df=df_xts)
date x y1 y2 y3 y4 y1_new
1 2007-01-01 17:00:00 1 0.8104089 -2.76764194 1.5904420 1.6583122 1.34258946
2 2007-01-02 17:00:00 2 1.3416652 3.97757263 6.2622732 8.3300956 0.84683353
3 2007-01-03 17:00:00 3 0.6925525 1.97349693 1.1367611 3.9290304 0.38163911
4 2007-01-04 17:00:00 4 -0.3231760 4.82490196 5.8738266 2.8540564 ...
这也适用于符号而不是字符串:
my_func(x = x, y = y1, y_new = y1_new, df = df_xts)
逐步运行该函数以更好地了解此处发生的情况可能很有用:
ensym
将输入转换为symbol
s:
x = 'x'
y = 'y1'
x <- rlang::ensym(x)
y <- rlang::ensym(y)
> x
x
> y
y1
substitute
根据替换symbol
表达式中的 slist(x=x,y=y)
并创建一个新表达式:
> substitute(lm(y ~ splines::ns(x,df=5),data=df),list(x=x,y=y))
lm(y1 ~ splines::ns(x, df = 5), data = df)
eval
计算新形成的表达式:
> eval(substitute(lm(y ~ splines::ns(x,df=5),data=df),list(x=x,y=y)))
Call:
lm(formula = y1 ~ splines::ns(x, df = 5), data = df)
Coefficients:
(Intercept) splines::ns(x, df = 5)1 splines::ns(x, df = 5)2
1.3426 -0.2424 -2.2221
splines::ns(x, df = 5)3 splines::ns(x, df = 5)4 splines::ns(x, df = 5)5
-0.6453 -3.4297 0.7092
这种技术广泛用于类似的包中ggplot2
,参见quasinotation:
library(ggplot2)
ggplot(df_xts)+geom_point(aes(x=x,y=y1))
推荐阅读
- excel - access和excel之间的双向数据传输
- swift - 从 UIGraphicsImageRenderer 创建图像时崩溃
- ios - TestFlight 构建未正确显示/消失
- html - 为什么我的第三个元素不在其父元素之外?
- sql - 在包含 TRY/CATCH 的测试过程中使用 tSQLt AssertEqualsTableSchema
- kotlin-exposed - 一种通过使用 uuid 访问实体来插入(选择)记录的方法,在数据库中具有 BINARY 形式的 uuid
- if-statement - 如何计算最弱条件?
- java - 这个视图的名称是什么,我如何在我的 android 应用程序中实现它
- browserstack - 如何在 BrowserStack app-automate 中获得实时网络调用?
- sql - 如何获取月份的最后一个日期