[R] Using string to call/manipulate an object

Jonathan P Daily jdaily at usgs.gov
Wed Feb 23 17:11:52 CET 2011


If you are looping through and have the names of the files as characters, 
say in ticker.names <- c("IBM", ...), you may be able to do this using:

for(nam in ticker.names)
{
[snip...]
        eval(parse(text = paste('colnames(', nam, ') <- c(
"open","high","low","close","vol")', sep = '')))
}

HTH,
Jon
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it."
     - Jubal Early, Firefly

r-help-bounces at r-project.org wrote on 02/23/2011 10:23:35 AM:

> [image removed] 
> 
> Re: [R] Using string to call/manipulate an object
> 
> Kushan Thakkar 
> 
> to:
> 
> David Winsemius
> 
> 02/23/2011 10:55 AM
> 
> Sent by:
> 
> r-help-bounces at r-project.org
> 
> Cc:
> 
> r-help
> 
> @Scott:
> 
> I can't just use
> 
> colnames(IBM) <- c("open","high","low","close","vol")
> 
> 
> I have nearly 100 tickers stored in an vector and I am looping through 
them.
> Its not a good idea to hardcode all of them.
> 
> @David:
> Column names is just an example. I have a long script that takes in an 
xts
> object and performs a series of manipulation. I want to to be able to 
run
> the same program on a series of xts objects through a loop. For that I 
need
> to be able to able to dynamically point/refer to those xts object inside 
my
> loop.
> 
> 
> On Wed, Feb 23, 2011 at 8:58 AM, David Winsemius 
> <dwinsemius at comcast.net>wrote:
> 
> >
> > On Feb 23, 2011, at 8:55 AM, Kushan Thakkar wrote:
> >
> >  I am using getSymbols function from quantmod package to get price 
data
> >> from
> >> internet.
> >>
> >> Currently I have:
> >>
> >> my.ticker <- "IBM"
> >> getSymbols(my.ticker,src="google")
> >>
> >> This  creates an xts object named my.ticker which contains historical
> >> price
> >> data for IBM.
> >>
> >> How can I call and manipulating this xts object using my original 
string
> >> my.ticker?
> >>
> >> I want to do:
> >> colnames(my.ticker) <- c("open","high","low","close","vol")
> >>
> >
> > If you want to change the colnames (an attribute) of the object now 
named
> > "IBM" in your global environment, then look a:
> >
> > ?attributes
> > ?attr
> >
> >
> >
> >> However, this does not work as my.ticker refers to the string "IBM" 
rather
> >> than the xts object called IBM.
> >>
> >> Out of desperation, I have also tried:
> >> colnames(paste(my.ticker)) <- c("open","high","low","close","vol")
> >> colnames(as.xts(my.ticker)) <- c("open","high","low","close","vol")
> >>
> >
> > But why are you do this??? It already has a perfectly servicable set 
of
> > names:
> >
> > > colnames(get(my.ticker))
> > [1] "IBM.Open"   "IBM.High"   "IBM.Low"    "IBM.Close"  "IBM.Volume"
> >
> > --
> > David.
> >
> >>
> >> Thanks
> >> Kushan
> >>
> >>        [[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.
> >>
> >
> > David Winsemius, MD
> > West Hartford, CT
> >
> >
> 
>    [[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.



More information about the R-help mailing list