Skip to content

Loop over several Variables, add on question

8 messages · bchr, Bert Gunter, PIKAL Petr +1 more

#
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
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
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
#
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.
#
Inline.
On Fri, Nov 2, 2012 at 5:34 AM, bchr <bochristoph at web.de> wrote:
Your claim is bogus. This is a very foolish strategy for "learning" R.
Go through the Introduction to R tutorial now, and you will save
yourself a lot of time and grief. Your confusion and wasted efforts
are caused by incorrect expectations about how R works, which is
**not** like the other software you mention.

To quote Rolf Turner on this list:

"
(1) Learn something about R; don't just hammer and hope.  Read the
introductory manuals and scan the FAQ.."

Cheers,
Bert

  Having a solid background in other

  
    
#
Hi

please put your messages in context. Not everybody is able/allowed to use Nabble.
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
#
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
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" .
____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!
#
Hey John,

thanks for making me notice that document - I just skipped over it the past
hour and I think I like that very condensed If you want to do this ->it
works like that approach. Besides that I had the impression, though that
it's content - while more strongly condensed - is basically not too
different from what is covered in R for Stata Users by the same author (and
his co-author), which I already have. But I might be wrong, so in case you
should know both books: would you think the R for SPSS/SAS-book will provide
a lot of additional info given that I have the R for Stata  already or will
they more or less overlap?

All the best

Bernhard



--
View this message in context: http://r.789695.n4.nabble.com/Loop-over-several-Variables-add-on-question-tp4648216p4648247.html
Sent from the R help mailing list archive at Nabble.com.