[R] looping through tasks

Patrick Burns pburns at pburns.seanet.com
Thu Feb 16 10:58:35 CET 2006


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
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

E. Michael Foster wrote:

>Hi,
>
>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
>
>
>  
>




More information about the R-help mailing list