Hey everyone, I have again a loop question: After generating the dataset using Jan?s approach from my previous posting (http://r.789695.n4.nabble.com/Loop-over-several-variables-td4648112.html) I want to rename the Variables in the new dataset so that all y will be called tiy. Doing it separately the following line works fine: ti<-rename (ti,c(y5="tiy5")) So I thought I simply extend to the following and the loop will be fine: ti<-rename (ti,c([paste0("y", 1:5)] = [paste0("tiy", 1:5)])) However, this will give me the following error message: Error: unexpected '[' in "ti<-rename (ti,c([" Removing the ?[? I then used the following line ti<-rename (ti,c(paste0("y", 1:5) = paste0("tiy", 1:5))) This provoked the following error: Error: unexpected '=' in "ti<-rename (ti,c(paste0("y", 1:5) =" I did some playing around with the brackets but I could not stop R from sending an error about the equal sign. Anybody any Idea where my mistake might be? Thanks in advance Bernhard -- View this message in context: http://r.789695.n4.nabble.com/Loop-over-several-Variables-add-on-question-tp4648216.html Sent from the R help mailing list archive at Nabble.com.
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of bchr > Sent: Friday, November 02, 2012 11:57 AM > To: r-help at r-project.org > Subject: [R] Loop over several Variables, add on question > > Hey everyone, > I have again a loop question: > After generating the dataset using Jan?s approach from my previous > posting > (http://r.789695.n4.nabble.com/Loop-over-several-variables- > td4648112.html) I want to rename the Variables in the new dataset so > that all y will be called tiy. Doing it separately the following line > works fine: > > ti<-rename (ti,c(y5="tiy5"))> ?renameNo documentation for ?rename? in specified packages and libraries: you could try ???rename?>Which rename function do you use? plyr, reshape, ... So I thought I simply extend to the following and the loop will be> fine: > > ti<-rename (ti,c([paste0("y", 1:5)] = [paste0("tiy", 1:5)])) > > However, this will give me the following error message: > > Error: unexpected '[' in "ti<-rename (ti,c([" > > Removing the ?[? I then used the following line > > ti<-rename (ti,c(paste0("y", 1:5) = paste0("tiy", 1:5))) > > This provoked the following error: > > Error: unexpected '=' in "ti<-rename (ti,c(paste0("y", 1:5) =" > > I did some playing around with the brackets but I could not stop R from > sending an error about the equal sign. Anybody any Idea where my > mistake might be?Maybe the mistake is that you did not read any R basics. What do you expect following command shall do? c(paste0("y", 1:5) = paste0("tiy", 1:5)) c expects several values and put them together to one object. You tried to fool it with paste0("y", 1:5) = paste0("tiy", 1:5) but above statement try to put result of paste0("tiy", 1:5) to paste0("y", 1:5) which I believe is not what do you want and which does not work either. For renaming names of unstated object ti you probably will be better with some grep construction. something like vec<-paste0("y", 1:5) vec [1] "y1" "y2" "y3" "y4" "y5" vec[grep("y",vec)]<- paste0("tiy", 1:5) vec [1] "tiy1" "tiy2" "tiy3" "tiy4" "tiy5" Regards Petr> > Thanks in advance > Bernhard > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Loop-over- > several-Variables-add-on-question-tp4648216.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.
Hey Petr, thanks for answering. First to your question: I use rename from the reshape package. You are probably right to assume that I do not have a to extensive reading background in R - basically I had to dive right in (which probably isn't a good idea, but I could not change that). Having a solid background in other statistics packages (stata, SPSS) I now find myself putting quite a lot of effort into presumably easy to do things like recoding, merging, looping etc. (I really dont want to mention how long it took me to get that recode done in the first place ...). So in any case I am sorry if I am bothering you with somewhat strange problems... Nevertehless even though your proposed solution did work 'as is' it did not when I remodelled it to my data vec<-paste0("y", 1:5) ti[grep("y",vec)]<- paste0("tiy", 1:5) It did produce no error message but the variable names did not change. Could the problem be that ti in my case is not a vector but a dataframe (the y's being variables in that dataframe?) All the best Bernhard -- View this message in context: http://r.789695.n4.nabble.com/Loop-over-several-Variables-add-on-question-tp4648216p4648225.html Sent from the R help mailing list archive at Nabble.com.
Thanks very much Petr, that one did just fine! I just wanted to say that it's not that I did no reading at all (in fact I use a book called R for Stata users by Muenchen/Hilbe and another German book called Programming in R when you translate the title) - so yes I am aware that R works quite differently than those other programms and that is what is causing me trouble, I guess. Anyway, I will also check the manual as you recommended, see if it helps. In any case thanks very much for your help Bernhard -- View this message in context: http://r.789695.n4.nabble.com/Loop-over-several-Variables-add-on-question-tp4648216p4648239.html Sent from the R help mailing list archive at Nabble.com.
In Line John Kane Kingston ON Canada> -----Original Message----- > From: bochristoph at web.de > Sent: Fri, 2 Nov 2012 05:34:48 -0700 (PDT) > To: r-help at r-project.org > Subject: Re: [R] Loop over several Variables, add on question > > Hey Petr, > > thanks for answering. First to your question: I use rename from the > reshape > package. > You are probably right to assume that I do not have a to extensive > reading > background in R - basically I had to dive right in (which probably isn't > a > good idea, but I could not change that). Having a solid background in > other > statistics packages (stata, SPSS) I now find myself putting quite a lot > of > effort into presumably easy to do things like recoding, merging, looping > etc.A solid background is SAS, & SPSS is a disadvantage in using R. I assume that the same applies for Stata. Google for "R for sas and spss users pdf" and read Bob's short comparison of the differences to look out for. An even better alternative but which may take longer is to get his book. http://onlinelibrary.wiley.com/doi/10.1111/j.1751-5823.2012.00179_24.x/abstract . Moving from the older stats packages to R is often mindrwrenching. After Bob's book you probably need to at least skim "Introduction to R" .>(I really dont want to mention how long it took me to get that > recode > done in the first place ...). So in any case I am sorry if I am bothering > you with somewhat strange problems... > > Nevertehless even though your proposed solution did work 'as is' it did > not > when I remodelled it to my data > vec<-paste0("y", 1:5) > ti[grep("y",vec)]<- paste0("tiy", 1:5) > It did produce no error message but the variable names did not change. > Could > the problem be that ti in my case is not a vector but a dataframe (the > y's > being variables in that dataframe?) > > All the best > > Bernhard > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Loop-over-several-Variables-add-on-question-tp4648216p4648225.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.____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more!