[R] subsetting with column name as string

Faheem Mitha faheem at email.unc.edu
Wed Aug 6 17:53:25 CEST 2008


Hi,

Consider the following

> x = c(1,2)
> y = c(3,4)
> d = data.frame(cbind(x,y))
> d$x
[1] 1 2
> d$"x"
[1] 1 2
>
> foo = function(val)
+ {
+   return(d$val)
+ }
>
> bar = function()
+ {
+   return(d$"x")
+ }
>
> foo("x")
NULL
> bar()
[1] 1 2

I'm a little surprised that R accepts both the form d$x and d$"x", but I'm 
mostly wondering why foo("x") doesn't work.
                                                            Thanks, Faheem.



More information about the R-help mailing list