2017-11-05 16 views
3

Rでは変数名の一部で変数を参照することができます。しかし、なぜ私がそれをすることができるのか混乱しています。変数名の一部で変数を参照

は、一例として、次のコードを使用します。私は、要約の構造(house.lm)を印刷するとき

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] 

、私は次の出力を得た:

> 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] 

これらはすべて同じ結果を与えますs:0.01029709

したがって、私はRの名前の一部のみを持つ変数を参照できるのでしょうか?

答えて

3

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

あなたは 完全な名前で、または名前の一部で、位置によって引数を指定することができます関数を呼び出します。引き数は最初に 正確な名前(完璧なマッチング)でマッチングされ、接頭辞マッチングによって、最後に ポジションによってマッチされます。

一部のデータを分析するためにクイックコーディングを行っているときに、部分名を使用することは問題ではありませんが、私は同意する傾向があります。あなたがそれを行うことはできませんパッケージでは、R-CMD checkすべての発生を見つけるでしょう。

5

名前の残りが明白でない場合は、これを実行できます。例えば

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$abcdf$abcdの両方を参照することができますので、正しいデータが得られますが、NULLdf$ab結果。これは、RStudioにdf$xyと入力してCtrl + Spaceを押すと、rigtht変数名が得られるので、変数名の一部を参照することができます。