繁体   English   中英

R:function 内的子集

[英]R: subsetting within a function

假设我在环境中有一个数据框,mydata,有三列,A、B、C。

mydata = data.frame(A=c(1,2,3),
                    B=c(4,5,6),
                    C=c(7,8,9))

我可以创建一个线性 model

lm(C ~ A, data=mydata)

我想要一个 function 来概括这一点,在 A 上回归 B 或 C,只给出列的名称,即

f = function(x){
  lm(x ~ A, data=mydata)
}
f(B)
f(C)

或者

g = function(x){
  lm(mydata$x ~ mydata$A)
}
g(B)
g(C)

这些解决方案不起作用。 我知道评估有问题,我尝试了 quo() 和 enquo() 和,. 的排列,但没有成功。

这是一个简化的示例,但我的想法是,当我要构建数十个类似的模型时,每个模型都相当复杂,只有一个变量发生变化,我希望这样做而不是每次都重复整个公式。

如果我们想传递不带引号的列名,并且选项是来自 tidyverse 的{{}} 使用select ,它可以接受字符串和不带引号的

library(dplyr)
printcol2 <- function(data, x) {
                    data %>%
                      select({{x}})
      }

printcol2(mydata, A)
#  A
#1 1
#2 2
#3 3
printcol2(mydata, 'A')
#  A
#1 1
#2 2
#3 3

如果 OP 想要传递未加引号的列名以在lm中传递

f1 <- function(x){
    rsp <- deparse(substitute(x))
    fmla <- reformulate("A", response = rsp)
    out <- lm(fmla, data=mydata)
    out$call <- as.symbol(paste0("lm(", deparse(fmla), ", data = mydata)"))
    out
   }

f1(B)

#Call:
#lm(B ~ A, data = mydata)

#Coefficients:
#(Intercept)            A  
#          3            1  

f1(C)

#Call:
#lm(C ~ A, data = mydata)

#Coefficients:
#(Intercept)            A  
#          6            1  

也许您正在寻找deparse(substitute(.)) 它接受引用或未引用的 arguments。

f = function(x, data = mydata){
  y <- deparse(substitute(x))
  fmla <- paste(y, 'Species', sep = '~')
  lm(as.formula(fmla), data = data)
}

mydata <- iris
f(Sepal.Length)
#
#Call:
#lm(formula = as.formula(fmla), data = data)
#
#Coefficients:
#      (Intercept)  Speciesversicolor   Speciesvirginica  
#            5.006              0.930              1.582  

f(Petal.Width)
#
#Call:
#lm(formula = as.formula(fmla), data = data)
#
#Coefficients:
#      (Intercept)  Speciesversicolor   Speciesvirginica  
#            0.246              1.080              1.780

我认为一般来说,您可能正在寻找:

printcol <- function(x){
  print(x)
}

printcol(mydata$A)

这不涉及任何花哨的评估,您只需要在 function 调用中指定要作为子集的变量。

这给了我们:

[1] 1 2 3

请注意,您只打印向量A ,而不是实际从mydata中对列A进行子集化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM