Re: [R] Re shaping a dataframe with conditional summary of columns - apply or reshape?

From: David Hewitt <>
Date: Thu, 24 Jan 2008 13:55:57 -0800 (PST)

> Try this:
> x$survey1[x$count==0] <- 0
> x$survey1[x$count %in% 1:11] <- 1
> x$survey1[x$count > 11] <- 2
> x.wide <- reshape(x, v.names="survey1", idvar="location",
> timevar="survey1", direction="wide")
> x.wide <- x.wide[order(x.wide$location),]
> x.wide$var1 <- tapply(x$var1, x$location, mean)
> x.wide

Thanks a lot Henrique. Only needed two simple changes: (1) Made the range in the second assignment 1:10 and the operator in the second > 10 to conform with the rules I needed. (2) Changed "timevar" from "survey1" to "survey". Using "survey1" reordered the columns for the survey observations, and it was much easier to have survey1 first, survey2 second, and survey3 third.

David Hewitt
Virginia Institute of Marine Science
View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 24 Jan 2008 - 21:59:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Jan 2008 - 22:30:09 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive