[R] evaluating a vector of characters

Greg Snow Greg.Snow at imail.org
Mon Jan 7 19:17:34 CET 2008


This is a fortune(106) situation (with a little fortune(77) in there as well).

I am unsure what exactly the original poster is wanting to do, but accessing the columns of a data frame (or list) by character strings can be done using [[ ]] rather than $ and should be able to work for this situation (and for data frames you can use matrix subscripting to get columns by name).

Try things like:

> tmp <- paste( 'x', 1:3, sep='' )
> Data[ , tmp ]
> for( i in seq(along=tmp) ) Data[[ tmp[i] ]]

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111
 
 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Henrique 
> Dallazuanna
> Sent: Monday, January 07, 2008 10:56 AM
> To: Gregory Gentlemen
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] evaluating a vector of characters
> 
> Try:
> sapply(paste("Data$x", 1:3, sep=""), function(x)eval(parse(text=x)))
> 
> On 07/01/2008, Gregory Gentlemen <gregory_gentlemen at yahoo.ca> wrote:
> > Dear R users,
> >
> > I'd like to evaluate a vector of characters. For example, 
> say I have a data frame called Data including the field names 
> x1, x2, x3, and I'd like to a list out of paste("Data$x", 
> 1:3, sep=""). How can I get list to evaluate paste("Data$x", 
> 1:3, sep="") as an R object rather than a string?
> >
> > Thanks in advance for you assistance.
> > Gregory
> >
> >
> > ---------------------------------
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide 
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> 
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 




More information about the R-help mailing list