Re: [R] simple question about variables....

From: Jacques VESLOT <jacques.veslot_at_good.ibl.fr>
Date: Thu 13 Jul 2006 - 18:32:19 EST

OBJ$"toto" works to...

 > b <- as.data.frame(matrix(1:4,2))
 > b
   V1 V2
1 1 3
2 2 4
 > b$"V1"
[1] 1 2

but varname is not evaluated in OBJ$varname.



Jacques VESLOT

CNRS UMR 8090
I.B.L (2ème étage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex

Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31

http://www-good.ibl.fr


Joerg van den Hoff a écrit :
> Stéphane Cruveiller wrote:
>

>>Dear R users,
>>
>>I have a simple question on variable manipulation.
>>Imagine I have an object "OBJ" that has "toto" as one of its variables.
>>I would like to understand why if I do
>>
>> > varname <- "toto"
>>
>> >OBJ$varname                             returns no results
>>
>>whereas
>>
>> > OBJ[varname]                            returns the column entitled 
>>"toto"
>>
>>
>>Thanks for your help.
>>
>>Stéphane.
>>

>
>
> because if the value of `varname' is substituted in the expressions, in
> the first case that yields
>
> OBJ$"toto" and in the second
> OBJ["toto"]
>
>
> the latter is valid, the former is not (you'd need `OBJ$toto' there),
> read ` ?"$" ':
>
> "...Both '[[' and '$' select a single element of the list. The main
> difference is that '$' does not allow computed indices, whereas
> '[[' does. 'x$name' is equivalent to 'x[["name"]]'..."
>
>
> not, too, the difference between `[' (sublist) and `[[' (single element
> extraction)
>
> joerg
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jul 13 18:36:13 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 13 Jul 2006 - 20:13:12 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.