Jinhua Wang
Jinhua Wang

Reputation: 1759

Refer to variable by part of the variable name

It seems that, in R, I can refer to a variable with part of a variable name. But I am confused about why I can do that.

Use the following code as an example:

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]

When I print the structure of summary(house.lm), I got the following output:

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

However, it seems that I can refer to the variable coefficients with all of the following commands:

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]

They all give the same results: 0.01029709

Therefore, I was wondering when I can refer to a variable with only part of its name in R?

Upvotes: 3

Views: 679

Answers (2)

Cedric
Cedric

Reputation: 2474

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

When calling a function you can specify arguments by position, by complete name, or by partial name. Arguments are matched first by exact name (perfect matching), then by prefix matching, and finally by position.

When you are doing quick coding to analyse some data, using partial names is not a problem, but I tend to agree, it's not good when writing code. In a package you can't do that, R-CMD check will find every occurence.

Upvotes: 3

Glaud
Glaud

Reputation: 733

You can do it when rest of name is unambiguous. For example

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 and even df$x gives right data, but df$ab results in NULL because it can refer to both df$abc and df$abcd. It's like when you type df$xy in RStudio and press Ctrl + Space you will get rigtht variable name, so you could refer to part of variable name.

Upvotes: 5

Related Questions