Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R函数中传递字符串_R_String_Function_Dataframe - Fatal编程技术网

如何在R函数中传递字符串

如何在R函数中传递字符串,r,string,function,dataframe,R,String,Function,Dataframe,我有一个df,我想用header和return线性模型的名称做一个函数 我正在尝试这个: a <- function(j,k){ reg1 <- lm(data$j ~ data$k) summary(reg1) } a(j="hour",k="score") a试试这个。如果要使用字符串作为数据帧中的变量,最好使用[[]]在函数中调用它们。下面是您的函数代码,稍有改动: a <- function(j,k){ reg1

我有一个df,我想用header和return线性模型的名称做一个函数

我正在尝试这个:

a <- function(j,k){
  reg1 <- lm(data$j ~ data$k)
  summary(reg1)
}

a(j="hour",k="score")

a试试这个。如果要使用字符串作为数据帧中的变量,最好使用
[[]]
在函数中调用它们。下面是您的函数代码,稍有改动:

a <- function(j,k){
  reg1 <- lm(data[[j]] ~ data[[k]])
  summary(reg1)
}

a(j="hour",k="score")

如果需要,您可以进一步调整函数
a

试试这个。如果要使用字符串作为数据帧中的变量,最好使用
[[]]
在函数中调用它们。下面是您的函数代码,稍有改动:

a <- function(j,k){
  reg1 <- lm(data[[j]] ~ data[[k]])
  summary(reg1)
}

a(j="hour",k="score")

如果需要,您可以进一步调整函数
a

将列名作为变量传递时,不能使用
$
。这里有几种方法可以做到这一点

  • 使用“重新格式化”创建公式对象
  • 您可以将其称为:

    a(mtcars, 'mpg', 'cyl')
    
    #Call:
    #lm(formula = sprintf("%s~%s", j, k), data = data)
    
    #Residuals:
    #    Min      1Q  Median      3Q     Max 
    #-4.9814 -2.1185  0.2217  1.0717  7.5186 
    
    #Coefficients:
    #            Estimate Std. Error t value Pr(>|t|)    
    #(Intercept)  37.8846     2.0738   18.27  < 2e-16 ***
    #cyl          -2.8758     0.3224   -8.92 6.11e-10 ***
    #---
    #Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    #Residual standard error: 3.206 on 30 degrees of freedom
    #Multiple R-squared:  0.7262,   Adjusted R-squared:  0.7171 
    #F-statistic: 79.56 on 1 and 30 DF,  p-value: 6.113e-10
    
    a(mtcars、mpg、cyl)
    #电话:
    #lm(公式=sprintf(“%s~%s”,j,k),数据=data)
    #残差:
    #最小1季度中值3季度最大值
    #-4.9814 -2.1185  0.2217  1.0717  7.5186 
    #系数:
    #估计标准误差t值Pr(>t)
    #(截距)37.8846 2.0738 18.27<2e-16***
    #气缸-2.8758 0.3224-8.92 6.11e-10***
    #---
    #签名。代码:0'***'0.001'***'0.01'*'0.05'.'0.1''1
    #剩余标准误差:30个自由度上的3.206
    #倍数R平方:0.7262,调整后的R平方:0.7171
    #F-统计量:1和30 DF的79.56,p-值:6.113e-10
    

    请注意,我在函数中添加了
    数据
    ,作为附加参数。通常,在函数中传递数据对象比在全局环境中依赖它进行求值更好。

    在将列名作为变量传递时,不能使用
    $
    。这里有几种方法可以做到这一点

  • 使用“重新格式化”创建公式对象
  • 您可以将其称为:

    a(mtcars, 'mpg', 'cyl')
    
    #Call:
    #lm(formula = sprintf("%s~%s", j, k), data = data)
    
    #Residuals:
    #    Min      1Q  Median      3Q     Max 
    #-4.9814 -2.1185  0.2217  1.0717  7.5186 
    
    #Coefficients:
    #            Estimate Std. Error t value Pr(>|t|)    
    #(Intercept)  37.8846     2.0738   18.27  < 2e-16 ***
    #cyl          -2.8758     0.3224   -8.92 6.11e-10 ***
    #---
    #Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    #Residual standard error: 3.206 on 30 degrees of freedom
    #Multiple R-squared:  0.7262,   Adjusted R-squared:  0.7171 
    #F-statistic: 79.56 on 1 and 30 DF,  p-value: 6.113e-10
    
    a(mtcars、mpg、cyl)
    #电话:
    #lm(公式=sprintf(“%s~%s”,j,k),数据=data)
    #残差:
    #最小1季度中值3季度最大值
    #-4.9814 -2.1185  0.2217  1.0717  7.5186 
    #系数:
    #估计标准误差t值Pr(>t)
    #(截距)37.8846 2.0738 18.27<2e-16***
    #气缸-2.8758 0.3224-8.92 6.11e-10***
    #---
    #签名。代码:0'***'0.001'***'0.01'*'0.05'.'0.1''1
    #剩余标准误差:30个自由度上的3.206
    #倍数R平方:0.7262,调整后的R平方:0.7171
    #F-统计量:1和30 DF的79.56,p-值:6.113e-10
    
    请注意,我在函数中添加了
    数据
    ,作为附加参数。通常,在函数中传递数据对象比在全局环境中依赖它进行评估更好

    a(mtcars, 'mpg', 'cyl')
    
    #Call:
    #lm(formula = sprintf("%s~%s", j, k), data = data)
    
    #Residuals:
    #    Min      1Q  Median      3Q     Max 
    #-4.9814 -2.1185  0.2217  1.0717  7.5186 
    
    #Coefficients:
    #            Estimate Std. Error t value Pr(>|t|)    
    #(Intercept)  37.8846     2.0738   18.27  < 2e-16 ***
    #cyl          -2.8758     0.3224   -8.92 6.11e-10 ***
    #---
    #Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    #Residual standard error: 3.206 on 30 degrees of freedom
    #Multiple R-squared:  0.7262,   Adjusted R-squared:  0.7171 
    #F-statistic: 79.56 on 1 and 30 DF,  p-value: 6.113e-10