Skip to content

Differences between "$" and "[["

5 messages · Eric Lecoutre, Duncan Murdoch, Peter Dalgaard +1 more

#
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.

Best,

Eric
Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium

tel: (+32)(0)10473050
lecoutre@stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

If the statistics are boring, then you've got the wrong numbers. -Edward 
Tufte
#
On Mon, 29 Nov 2004 15:47:58 +0100, Eric Lecoutre
<lecoutre@stat.ucl.ac.be> wrote :
I think you do understand it.  From the line above, l$p would be
equivalent to l[["p"]], which is clearly different from l[[p]].

But since l$"text-align" is harder to construct than l[["text-align"]]
when "text-align" is stored in p, why bother?

Duncan Murdoch
#
Eric Lecoutre <lecoutre@stat.ucl.ac.be> writes:
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...).
#
Eric Lecoutre <lecoutre <at> 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.

[[ evaluates its right argument and $ does not.  The "..." notation is
just to allow one to specify non-syntactic arguments.  One could
alternately use l$`text-align` .  I think the "..." notation
is a holdover from before `...` was implemented.

Its also possible to define your own class and have $ operate any
way you like on it (although its probably best to stick with
standard behavior and the following is not really recommended):

	l<-list("text-align"="right", a=2)
	class(l) <- c("mylist", "list")
	"$.mylist" <- function(x, idx) { 
	     y <- x[[idx]]
	     if (is.null(y)) x[[eval.parent(parse(text=idx))]] else y
	}
	p <- "text-align"
	l$p # "right"
	l$"text-align" # same
	l$`text-align` # same
	a <- 99
	l$a # 2
	l$"a" # same
	l$`a` # same
	l[["a"]] # same
	l[[a]] # 99
#
Gabor Grothendieck <ggrothendieck <at> myway.com> writes:

: 
: Eric Lecoutre <lecoutre <at> 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.
: 
: [[ evaluates its right argument and $ does not.  The "..." notation is
: just to allow one to specify non-syntactic arguments.  One could
: alternately use l$`text-align` .  I think the "..." notation
: is a holdover from before `...` was implemented.
: 
: Its also possible to define your own class and have $ operate any
: way you like on it (although its probably best to stick with
: standard behavior and the following is not really recommended):
: 
: 	l<-list("text-align"="right", a=2)
: 	class(l) <- c("mylist", "list")
: 	"$.mylist" <- function(x, idx) { 
: 	     y <- x[[idx]]
: 	     if (is.null(y)) x[[eval.parent(parse(text=idx))]] else y
: 	}
: 	p <- "text-align"
: 	l$p # "right"
: 	l$"text-align" # same
: 	l$`text-align` # same
: 	a <- 99
: 	l$a # 2
: 	l$"a" # same
: 	l$`a` # same
: 	l[["a"]] # same
: 	l[[a]] # 99

The comment on the last one is wrong.  It should give an error since
there is no 99th component to l.