[R] Proper Paste for Data Member

Greg Snow Greg.Snow at imail.org
Wed Jul 15 22:03:52 CEST 2009


The dollar sign ($) is a magical shortcut for the [[ subset operator (yes the doubling is intentional) for accessing parts of lists/data frames/etc.  When you try to use magical shortcuts for purposes beyond what they were designed for, you get the computer equivalent of turning yourself into a chicken.  It is better to go back to the original operator rather than trying to get the magical shortcut to do what you want.  Your solution may be as simple as:

> tsSource <- ts( sh[[NAMEVARIABLE]] )

And the like.

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 Idgarad
> Sent: Tuesday, July 14, 2009 12:02 PM
> To: r-help at r-project.org
> Subject: [R] Proper Paste for Data Member
> 
> I imported a spreadsheet into a variable sh
> 
> e.g. sh$aaaa, sh$bbbb, etc...
> 
> doing the following:
> 
> tsSource <- ts(paste("sh$",NAMEVARIABLE,sep="") ... )
> 
> fails. The paste isn't evaluating properly. What is the proper way to
> concatenate a data source with a member name such that they evaluate
> properly.
> 
> actual code below:
> doEnv <- function(SOURCEDATA,REGDATA,HOUR,ENVNAME,REPORTNAME) {
> print(SOURCEDATA)
> print(REGDATA)
> print(HOUR)
> print(ENVNAME)
> print(REPORTNAME)
> # blah blah blah ...
> 
> #Raw Data
> channel1 <- odbcConnectExcel("Q:/metrics.xls")
> sqlTables(channel1)
> sh1 <- sqlFetch(channel1, "Actuals$")
> close(channel1)
> 
> # Something here is borked like the Chef himself....
> tsSource<-ts(paste("sh1$",ENVNAME,sep=""),start=c(2004,1),freq=52)
> print(tsSource)
> plot(tsSource,col="grey",type="n")
> return("AUTOBOT") # I use AUTOBOT or DECEPTICON for generic pass fail
> return
> values. Yes I am a geek...
> }
> 
> 	[[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