Re: [R] looping through tasks

From: Patrick Burns <>
Date: Thu 16 Feb 2006 - 20:58:35 EST

S Poetry is one place to look for hints about R programming. Some of the details are not quite right, and there are simple solutions to some of the topics, but for the most part it is good for R.

Patrick Burns
+44 (0)20 8525 0696
(home of S Poetry and "A Guide for the Unwilling S User")

E. Michael Foster wrote:

>I'm moving (slowly) to R from STATA.
>I often have need to move through a set of tasks across a series of years.
>In this case, you can see that I'm mimicking -reshape- in STATA, but I'm
>less interested in the
>task than in programming R.
> library(foreign)
> mydata<-read.dta("z:\example.dta")
> for (y in 2000:2002) {
> myvar<-paste("score",y,sep="") # x is available for each year
> assign( eval(myvar),
> data.frame(cbind(mydata[,c("var1", eval(myvar))],c(eval(y)))))
> colnames(eval(myvar))<-c("person","score","year")
> }
>I"m getting the error, "
>Error in "colnames<-"(`*tmp*`, value = c("newid", "score", "year")) :
>attempt to set colnames on object with less than two dimensions
>Whether I set up a data frame or not doesn't matter.
>As you might guess, what I want to do at the end is rbind the little
>files, and the lack of consistent column names causes the program to choke.
>Suggestions? /m
> mailing list PLEASE do read the posting guide! Received on Thu Feb 16 21:04:18 2006

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 14:08:29 EST