Assigning a value to a variable that has a "dot" in the name

I am new to R and have tried the following code. To my surprise, assigning something to ret$log.id actually result in the same value being assigned to ret$log For instance.

  ret <- c() ret$log.id <- 'a' 

Making the next return "a"

 ret$log 

Is that what R should do? I hope someone can give me some idea about this.

Thanks,

+4
source share
3 answers

This is normal behavior:

 x = data.frame(happy = rnorm(10), sad = rnorm(10)) > x$hap [1] -0.9373243 -0.9497992 -0.1413024 -0.9857493 1.7156495 0.8715162 0.8377111 [8] -0.4161816 -0.3976979 -0.2569765 

I think Chase is right - a partial match in the game.

Interestingly, if there are two columns that correspond to a partial match, then NULL is indicated instead of a warning:

 y = data.frame(happy = rnorm(10), sad = rnorm(10), sadder = rnorm(10)) > y$sa NULL 
+4
source

Yes, the $ operator does partial matching. You can examine the behavior a bit with the following:

 ret <- c() ret$log.id <- "a" ret$l #Returns "a" ret$log.at <- "b" 

Now let's see what happened:

 ret$l ret$log ret$log.i ret$log.a 
+6
source

Think of a partial match. On the help page for $ :

In the "Arguments" section:

 name A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under 'Environments') partially matched to the names of the object. 

and then under the character indices:

 Character indices can in some circumstances be partially matched (see pmatch) to the names or dimnames of the object being subsetted (but never for subassignment). 

Also under symbolic indices:

 Thus the default behaviour is to use partial matching only when extracting from recursive objects (except environments) by $. Even in that case, warnings can be switched on by options(warnPartialMatchAttr = TRUE). 

See names and pmatch more details, but this cleared it for me.

+6
source

All Articles