Cecile
Cecile

Reputation: 535

issue with variable names including dots

There is something I don't understand when calling dataframes column names. For example:

x1<- data.frame(a.variable=c('1','2','3'), b.variable=c('10','20','30'))
x1$a.variable 
# returns [1] 1 2 3 which makes sense

However, the line below doesn't make sense to me as the column "a" doesn't exists.

x1$a
# returns [1] 1 2 3

Could someone help me understand how to avoid that issue? Thanks!

Upvotes: 2

Views: 71

Answers (1)

markus
markus

Reputation: 26353

The mentioned behavior describes one important difference between $ and [[. $ does partial matching, and [[ does not (by default). This can be controlled however using the exact argument, see help(`[`):

x1[["a"]]
# NULL

x1[["a", exact = FALSE]]
# [1] "1" "2" "3"

Upvotes: 4

Related Questions