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@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.