From p.dalgaard at biostat.ku.dk  Mon Nov 29 16:19:24 2004
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Mon Nov 29 16:21:50 2004
Subject: [Rd] Differences between "$" and "[["
In-Reply-To: <6.0.1.1.2.20041129153016.0393fec0@stat4ux.stat.ucl.ac.be>
References: <6.0.1.1.2.20041129153016.0393fec0@stat4ux.stat.ucl.ac.be>
Message-ID: <x2u0r8k7ib.fsf@biostat.ku.dk>

Eric Lecoutre <lecoutre@stat.ucl.ac.be> writes:

> Hi,
> 
> If I define the following list:
> 
>  > (l<-list("text-align"="right"))
> $"text-align"
> [1] "right"
> 
> I know that I can't use l$text-align, as the parser will find a '-' operation.
> If I want (need) to use special names, as "text-align", I have to
> enclose it between "". So I can use:
> 
> l$"text-align" or l[["text-align"]]
> 
> If now I have the text "text-align" defined in a variable:
> p<-"text-align"
> 
> I can use:
>  > l[[p]]
> [1] "right"
> 
> But I can't use l$p
> 
> where as it is said in the help page that 'x$name' is equivalent to
> 'x[["name"]]'.
> 
> Anyway I will use "[[" but I dont clearly understand this behavior.

It also says

     The operators '$' and '$<-' do not evaluate their second argument.
      It is translated to a string and that string is used to locate
     the correct component of the first argument.

so l$p looks for l[["p"]] since the p is treated as a symbol. The fact
that there is an object called p and that it contains a special name
is immaterial (the opposite would be truly scary...).

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907


[Rd] Differences between "$" and "[["
Peter Dalgaard