Loop over several Variables, add on question
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"))
?rename
No 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.