Re: [R] looping through tasks

From: ronggui <>
Date: Thu 16 Feb 2006 - 15:41:14 EST

R has the reshape function do the familiar task as -reshape- in stata.


     This function reshapes a data frame between 'wide' format with
     repeated measurements in separate columns of the same record and
     'long' format with the repeated measurements in separate records.

2006/2/16, E. Michael Foster <>:
> 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
> --
> ________________________________________________
> E. Michael Foster
> download vcard:
> Professor of Maternal and Child Health
> of Health Policy and
> Administration
> Associate Chair for Faculty Advancement
> (W) 919-966-3773
> (F) 919-966-0458
> Office: 407C Rosenau Hall << use for FEDEX
> School of Public Health
> University of North Carolina, Chapel Hill
> Rosenau Hall, Campus Box# 7445
> Chapel Hill, NC 27599-7445
> Visit the new FAQ section of my web site where I
> address questions such as "What's on your IPOD?" or
> "Who's your person of the year?"
> "What do you mean, this jacket makes me look like John Kerry on a pheasant hunt. I'll fill your ass with bird shot, you old coot."
> -- Dick Cheney, just before shooting his homie.
> "But the one thing we can all agree, all faiths and ideologies, is that God is with the vulnerable and poor. God is in the slums, in the cardboard boxes where the poor play house”­ God is in the silence of a mother who has infected her child with a virus that will end both their lives”­ God is in the cries heard under the rubble of war”­ God is in the debris of wasted opportunity and lives, and God is with us if we are with them. 'If you remove the yolk from your midst, the pointing of the finger and speaking wickedness, and if you give yourself to the hungry and satisfy the desire of the afflicted, then your light will rise in darkness and your gloom with become like midday and the Lord will continually guide you and satisfy your desire in scorched places'
> It's not a coincidence that in the Scriptures, poverty is mentioned more than 2,100 times. It's not an accident. That's a lot of air time, 2,100 mentions. [You know, the only time Christ is judgmental is on the subject of the poor.] 'As you have done it unto the least of these my brethren, you have done it unto me.' (Matthew 25:40). As I say, good news to the poor."
> - Bono
> ______________________________________________
> mailing list
> PLEASE do read the posting guide!

Deparment of Sociology
Fudan University

______________________________________________ mailing list PLEASE do read the posting guide!

Received on Thu Feb 16 15:46:19 2006

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