stefan.duke at gmail.com
2010-Oct-07 17:05 UTC
[R] reshape from wide to long, ordering of "varying"
Hello, I have data in the following form age sex Int.Prev.Est.1 Int.Prev.Est.2 Int.Prev.Est.3 Int.Prev.Est.4 Int.Prev.Est.5 93110 93 0 23.75482 57.86592 9.755003 4.343534 4.280714 93610 93 1 53.36475 39.47247 4.381618 1.622119 1.159044 94110 94 0 23.47514 58.23936 10.789339 3.690415 3.805741 94610 94 1 53.34524 39.32675 4.602774 1.520247 1.204988 95110 95 0 23.76256 58.18757 9.583178 4.186825 4.279866 95610 95 1 53.14037 39.55376 4.313944 1.632805 1.359124 and want to get it in the form: age sex cat Int.Prev.Est 93 0 1 23.75482 93 0 2 57.86592 93 0 3 9.755003 93 0 4 4.343534 (....) 95 1 4 1.632805 95 1 5 1.359124 I am getting close with reshape, but somehow the categorie varialbe is orderd 1...1, 2...2,3...3,4...4,5....5 instad as 1,2,3,4,5,1,2,3.... reshape(UK.INT, idvar=1:2, times=1:5, varying=c("Int.Prev.Est.1", "Int.Prev.Est.2", "Int.Prev.Est.3", "Int.Prev.Est.4", "Int.Prev.Est.5"),v.names='cat', direction="long") # Here is some example data dput(tail(UK.INT)) structure(list(age = c(93, 93, 94, 94, 95, 95), sex = c(0, 1, 0, 1, 0, 1), Int.Prev.Est.1 = c(2647L, 19706L, 1832L, 15229L, 1277L, 11456L), Int.Prev.Est.2 = c(6448L, 14576L, 4545L, 11227L, 3127L, 8527L), Int.Prev.Est.3 = c(1087L, 1618L, 842L, 1314L, 515L, 930L), Int.Prev.Est.4 = c(484L, 599L, 288L, 434L, 225L, 352L), Int.Prev.Est.5 = c(477L, 428L, 297L, 344L, 230L, 293L)), .Names = c("age", "sex", "Int.Prev.Est.1", "Int.Prev.Est.2", "Int.Prev.Est.3", "Int.Prev.Est.4", "Int.Prev.Est.5"), row.names = c("93110", "93610", "94110", "94610", "95110", "95610"), class = "data.frame") I am wondering whether this is really feasible with reshape. Thanks for any hint. Best, stefan
Henrique Dallazuanna
2010-Oct-07 17:13 UTC
[R] reshape from wide to long, ordering of "varying"
Try this: reshape(UK.INT, direction = 'long', varying = list(grep("Int.Prev.Est", names(UK.INT)))) On Thu, Oct 7, 2010 at 2:05 PM, stefan.duke@gmail.com <stefan.duke@gmail.com> wrote:> Hello, > I have data in the following form > > age sex Int.Prev.Est.1 Int.Prev.Est.2 Int.Prev.Est.3 > Int.Prev.Est.4 Int.Prev.Est.5 > 93110 93 0 23.75482 57.86592 9.755003 > 4.343534 4.280714 > 93610 93 1 53.36475 39.47247 4.381618 > 1.622119 1.159044 > 94110 94 0 23.47514 58.23936 10.789339 > 3.690415 3.805741 > 94610 94 1 53.34524 39.32675 4.602774 > 1.520247 1.204988 > 95110 95 0 23.76256 58.18757 9.583178 > 4.186825 4.279866 > 95610 95 1 53.14037 39.55376 4.313944 > 1.632805 1.359124 > > and want to get it in the form: > > age sex cat Int.Prev.Est > 93 0 1 23.75482 > 93 0 2 57.86592 > 93 0 3 9.755003 > 93 0 4 4.343534 > (....) > 95 1 4 1.632805 > 95 1 5 1.359124 > > > I am getting close with reshape, but somehow the categorie varialbe is > orderd 1...1, 2...2,3...3,4...4,5....5 instad as 1,2,3,4,5,1,2,3.... > > reshape(UK.INT, idvar=1:2, times=1:5, varying=c("Int.Prev.Est.1", > "Int.Prev.Est.2", "Int.Prev.Est.3", "Int.Prev.Est.4", > "Int.Prev.Est.5"),v.names='cat', direction="long") # > > > Here is some example data > > dput(tail(UK.INT)) > > structure(list(age = c(93, 93, 94, 94, 95, 95), sex = c(0, 1, > 0, 1, 0, 1), Int.Prev.Est.1 = c(2647L, 19706L, 1832L, 15229L, > 1277L, 11456L), Int.Prev.Est.2 = c(6448L, 14576L, 4545L, 11227L, > 3127L, 8527L), Int.Prev.Est.3 = c(1087L, 1618L, 842L, 1314L, > 515L, 930L), Int.Prev.Est.4 = c(484L, 599L, 288L, 434L, 225L, > 352L), Int.Prev.Est.5 = c(477L, 428L, 297L, 344L, 230L, 293L)), .Names > = c("age", > "sex", "Int.Prev.Est.1", "Int.Prev.Est.2", "Int.Prev.Est.3", > "Int.Prev.Est.4", "Int.Prev.Est.5"), row.names = c("93110", "93610", > "94110", "94610", "95110", "95610"), class = "data.frame") > > I am wondering whether this is really feasible with reshape. Thanks > for any hint. > Best, > stefan > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Gabor Grothendieck
2010-Oct-07 17:19 UTC
[R] reshape from wide to long, ordering of "varying"
On Thu, Oct 7, 2010 at 1:05 PM, stefan.duke at gmail.com <stefan.duke at gmail.com> wrote:> Hello, > I have data in the following form > > ? ? ?age sex Int.Prev.Est.1 Int.Prev.Est.2 Int.Prev.Est.3 > Int.Prev.Est.4 Int.Prev.Est.5 > 93110 ?93 ? 0 ? ? ? 23.75482 ? ? ? 57.86592 ? ? ? 9.755003 > 4.343534 ? ? ? 4.280714 > 93610 ?93 ? 1 ? ? ? 53.36475 ? ? ? 39.47247 ? ? ? 4.381618 > > and want to get it in the form: > > age sex cat ?Int.Prev.Est > 93 ? 0 ? ? 1 ? 23.75482 > 93 ? 0 ? ? 2 ? 57.86592 > 93 ? 0 ? ? 3 ? 9.755003 > 93 ? 0 ? ? 4 ? 4.343534 > (....)Try this: reshape(UK.INT, dir = "long", idvar = 1:2, varying = list(3:7), v.names = "Int.Prev.Est", timevar = "cat") -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
stefan.duke at gmail.com
2010-Oct-07 20:42 UTC
[R] reshape from wide to long, ordering of "varying"
Thanks a lot! Both solutions work great! On Thu, Oct 7, 2010 at 7:19 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On Thu, Oct 7, 2010 at 1:05 PM, stefan.duke at gmail.com > <stefan.duke at gmail.com> wrote: >> Hello, >> I have data in the following form >> >> ? ? ?age sex Int.Prev.Est.1 Int.Prev.Est.2 Int.Prev.Est.3 >> Int.Prev.Est.4 Int.Prev.Est.5 >> 93110 ?93 ? 0 ? ? ? 23.75482 ? ? ? 57.86592 ? ? ? 9.755003 >> 4.343534 ? ? ? 4.280714 >> 93610 ?93 ? 1 ? ? ? 53.36475 ? ? ? 39.47247 ? ? ? 4.381618 >> >> and want to get it in the form: >> >> age sex cat ?Int.Prev.Est >> 93 ? 0 ? ? 1 ? 23.75482 >> 93 ? 0 ? ? 2 ? 57.86592 >> 93 ? 0 ? ? 3 ? 9.755003 >> 93 ? 0 ? ? 4 ? 4.343534 >> (....) > > Try this: > > reshape(UK.INT, dir = "long", idvar = 1:2, varying = list(3:7), > v.names = "Int.Prev.Est", timevar = "cat") > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com >