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

David Hewitt dhewitt37 at gmail.com
Thu Jan 24 22:55:57 CET 2008




> 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
http://www.vims.edu/fish/students/dhewitt/
-- 
View this message in context: http://www.nabble.com/Reshaping-a-dataframe-with-conditional-summary-of-columns---apply-or-reshape--tp15058600p15075986.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list