[R] subsetting with column name as string

Faheem Mitha
Wed, 06 Aug 2008 15:53:25 -0000

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.