[R-sig-Epi] converting string to a object name of a dataset

BXC (Bendix Carstensen) bxc at steno.dk
Fri Mar 18 08:19:47 CET 2011


get() looks in the search path, so you can put data 1 there using attach():

> data1<- data.frame(x=c(1:4), y=rep(1,4))
> attach(data1)
> gvars<- c("x", "y")
> get( gvars[1] )
[1] 1 2 3 4
> get( gvars[2] )
[1] 1 1 1 1

Best regards,
Bendix Carstensen
_________________________________________

Bendix Carstensen 
Senior Statistician
Steno Diabetes Center A/S
Niels Steensens Vej 2-4
DK-2820 Gentofte
Denmark
+45 44 43 87 38 (direct)
+45 30 75 87 38 (mobile)
bxc at steno.dk    www.biostat.ku.dk/~bxc
www.steno.dk

> -----Original Message-----
> From: r-sig-epi-bounces at r-project.org 
> [mailto:r-sig-epi-bounces at r-project.org] On Behalf Of Madan 
> Gopal Kundu
> Sent: 18. marts 2011 06:19
> To: r-sig-epi at r-project.org
> Subject: [R-sig-Epi] converting string to a object name of a dataset
> 
> Hi,
> 
> I am trying to access object which is variable of a dataset 
> using string.  For that I am trying to convert a string to 
> the object name in the following code. But unfortunately it 
> looks something is going wrong in the following code.
> 
> data1<- data.frame(x=c(1:4), y=rep(1:4))
> gvars<- c("x", "y")
> z<- paste("data1", gvars[1], sep="$")
> get(z)
> 
> The above code should return following output:
> [1] 1 2 3 4
> 
> but instead it is printing following error "Error in get(z) : 
> object 'data1$x' not found"
> 
> Please help me to fix the problem.
> 
> Regards
> Madan
> 
> _______________________________________________
> R-sig-Epi at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-epi
> 


More information about the R-sig-Epi mailing list