按变量名称的一部分引用变量



似乎,在R中,我可以用变量名称的一部分来引用变量。但我对为什么我可以这样做感到困惑。

以以下代码为例:

library(car)
scatterplot(housing ~ total)
house.lm <- lm(housing ~ total)
summary(house.lm)
str(summary(house.lm))
summary(house.lm)$coefficients[2,2]
summary(house.lm)$coe[2,2]

当我打印摘要(house.lm)的结构时,我得到了以下输出:

> str(summary(house.lm))
List of 11
 $ call         : language lm(formula = housing ~ total)
 $ terms        :Classes 'terms', 'formula'  language housing ~ total
  .. ..- attr(*, "variables")= language list(housing, total)
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "housing" "total"
  .. .. .. ..$ : chr "total"
  .. ..- attr(*, "term.labels")= chr "total"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(housing, total)
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. ..- attr(*, "names")= chr [1:2] "housing" "total"
 $ residuals    : Named num [1:162] -8.96 -11.43 3.08 8.45 2.2 ...
  ..- attr(*, "names")= chr [1:162] "1" "2" "3" "4" ...
 $ coefficients : num [1:2, 1:4] 28.4523 0.0488 10.2117 0.0103 2.7862 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "(Intercept)" "total"
  .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
 $ aliased      : Named logi [1:2] FALSE FALSE
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "total"
 $ sigma        : num 53.8
 $ df           : int [1:3] 2 160 2
 $ r.squared    : num 0.123
 $ adj.r.squared: num 0.118
 $ fstatistic   : Named num [1:3] 22.5 1 160
  ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
 $ cov.unscaled : num [1:2, 1:2] 3.61e-02 -3.31e-05 -3.31e-05 3.67e-08
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "(Intercept)" "total"
  .. ..$ : chr [1:2] "(Intercept)" "total"
 - attr(*, "class")= chr "summary.lm"

但是,我似乎可以使用以下所有命令来引用变量系数:

summary(house.lm)$coe[2,2]
summary(house.lm)$coef[2,2]
summary(house.lm)$coeff[2,2]
summary(house.lm)$coeffi[2,2]
summary(house.lm)$coeffic[2,2]
summary(house.lm)$coeffici[2,2]
summary(house.lm)$coefficie[2,2]
summary(house.lm)$coefficien[2,2]
summary(house.lm)$coefficient[2,2]
summary(house.lm)$coefficients[2,2]

它们都给出相同的结果:0.01029709

因此,我想知道什么时候我可以在 R 中引用一个只有部分名称的变量?

当名称的其余部分明确时,您可以执行此操作。例如

df <- data.frame(abcd = c(1,2,3), xyz = c(4,5,6), abc = c(5,6,7))
> df$xy
[1] 4 5 6
> df$ab
NULL
> df$x
[1] 4 5 6

df$xy甚至df$x都提供了正确的数据,但df$ab会导致NULL,因为它可以同时引用df$abcdf$abcd。这就像当您在 RStudio 中键入 df$xy 并按 Ctrl + 空格键时,您将获得 rigtht 变量名称,因此您可以引用变量名称的一部分。

http://adv-r.had.co.nz/Functions.html#lexical-scoping

调用函数时,您可以按位置指定参数,通过 全名,或按部分名称。参数首先匹配 精确名称(完美匹配),然后通过前缀匹配,最后通过 位置。

当你做快速编码来分析一些数据时,使用部分名称不是问题,但我倾向于同意,这在编写代码时并不好。在包中,你不能这样做,R-CMD check会找到每一个出现的情况。

最新更新