[R] equivalent code that doesn't return same results?

William Dunlap wdunlap at tibco.com
Wed Jan 16 23:23:10 CET 2013


>> eval(parse(text=paste("dataset",IVcat[k],sep="$")))<-relevel(eval(parse(text=paste("dataset",IVcat[k],sep="$"))),ref="online")
>This code returns the following error:
>Error in eval(parse(text = paste("dataset", IVcat[k], sep = "$"))) <-
>relevel(eval(parse(text = paste("dataset",  : 
>  target of assignment expands to non-language object

Replace
    eval(parse(text=paste("dataset", IVcat[k], sep="$")))
with
    dataset[[ IVcat[k] ]]
everywhere.

This works because
    object$componentName
is the same as
    object[[ "componentName" ]]
and the "[[" version lets you use a variable for the name of the component.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of cwladis
> Sent: Wednesday, January 16, 2013 1:12 PM
> To: r-help at r-project.org
> Subject: [R] equivalent code that doesn't return same results?
> 
> I need to automate changing the reference value for factors in analysis, and
> it is my understanding that the following two sets of code should produce
> identical results, but they do not:
> 1)dataset$method
> 2)eval(parse(text=paste("dataset",IVcat[k],sep="$")))        (in this case I
> have IVcat[k]=method, which is why they are equal)
> 
> I even tested them in R, which says that these two objects are identical:
> > identical(dataset$method,eval(parse(text=paste("dataset",IVcat[k],sep="$"))))
> [1] TRUE
> 
> However, when I write the following code, which is the same in both cases
> (except that the first case uses expression (1) above and the second case
> uses expression (2)), it works in the first case but returns an error code
> in the second:
> > dataset$method<-relevel(dataset$method,ref="online")
> This code properly changes the reference level as desired.
> 
> > eval(parse(text=paste("dataset",IVcat[k],sep="$")))<-
> relevel(eval(parse(text=paste("dataset",IVcat[k],sep="$"))),ref="online")
> This code returns the following error:
> Error in eval(parse(text = paste("dataset", IVcat[k], sep = "$"))) <-
> relevel(eval(parse(text = paste("dataset",  :
>   target of assignment expands to non-language object
> 
> I'm new to R, so maybe I'm making a stupid mistake here (maybe because I'm
> still not understanding the relevel function properly?), but I've been
> banging my head against this for most of the day, and I'm out of ideas...
> 
> Thanks for taking the time to read my post, and thanks in advance for any
> help!
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/equivalent-code-that-
> doesn-t-return-same-results-tp4655788.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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