Re: [R] looping through tasks

From: Patrick Burns <pburns_at_pburns.seanet.com>
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
patrick@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
>
>
>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html 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