Skip to content
Prev 255 / 585 Next

converting string to a object name of a dataset

Hi,

Thanks to David and Bendix for giving me the solution. I would like to
extend my question little bit further. I am trying to execute
following code:

data1<- data.frame(x=c(1:4), y=rep(1:4))
gvars<- c("x", "y")
for (i in 1:2)
{
print(data1[[gvars[i]]])
}

and it returns the following output:
[1] 1 2 3 4
[1] 1 2 3 4

However, my desired output is
[1] 1 2 3 4
[1] 1 1 1 1

I tried the following code as well, but the problem remains same:
attach(data1)
for (i in 1:2)
{
print(get(gvars[i]))
}

Thanks in advance!!

Regards
Madan
On Fri, Mar 18, 2011 at 3:19 AM, BXC (Bendix Carstensen) <bxc at steno.dk> wrote: