[R] reshape from wide to long, ordering of "varying"
stefan.duke at gmail.com
stefan.duke at gmail.com
Thu Oct 7 19:05:09 CEST 2010
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
More information about the R-help
mailing list