[R] Loop over several Variables, add on question

PIKAL Petr petr.pikal at precheza.cz
Fri Nov 2 14:54:44 CET 2012


Hi

please put your messages in context. Not everybody is able/allowed to use Nabble.

> -----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 1:35 PM
> 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. (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?)

Hm. No data, no structure of ti. Hm. Hm. Just let me look into my crystal ball. You want to change names of columns in data frame ti

names(ti) gives you a vector of names

names(ti)[grep("y", names(ti))]

selects relevant names

names(ti)[grep("y", names(ti))] <- paste0("tiy", 1:5)

changes respective names.

If you read only few pages (about 20) from R-intro, which you shall have installed together with R, you probably will find many basic operations with objects, distinctions among various object types and how to check your objects. The reading shall not take so long and maybe you will find after that reading it was the best spent time ever in your R learning.

Regards
Petr

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




More information about the R-help mailing list