Dear R helpers,
xx = data.frame(country = c("USA", "UK",
"Canada"), x = c(10, 50, 20), y = c(40, 80, 35), z = c(70, 62, 10))
> xx
country x y z
1 USA 10 40 70
2
UK 50 80 62
3 Canada 20 35 10
I need to arrange this as a new data.frame as follows -
country type values
USA x 10
USA y 40
USA z 70
UK x 50
UK y 80
UK z 62
Canada x 20
Canada y 35
Canada z 10
I did try reshape package but things are in mess. Please guide
Regards
Vincy
[[alternative HTML version deleted]]
reshape2 Like this?> require(reshape2)Loading required package: reshape2> melt(xx)Using country as id variables country variable value 1 USA x 10 2 UK x 50 3 Canada x 20 4 USA y 40 5 UK y 80 6 Canada y 35 7 USA z 70 8 UK z 62 9 Canada z 10>On Wed, Mar 9, 2011 at 7:45 PM, Vincy Pyne <vincy_pyne at yahoo.ca> wrote:> Dear R helpers, > > xx = data.frame(country = c("USA", "UK", "Canada"), x = c(10, 50, 20), y = c(40, 80, 35), z = c(70, 62, 10)) > >> xx > ? ? ?? country????? x???? y??? z > 1????? USA??????? 10??? 40? 70 > 2 > ?UK????????? 50?? 80?? 62 > 3???? Canada??? 20?? 35?? 10 > > > > > I need to arrange this as a new data.frame as follows - > > country?????? type???? values > USA??????????? x???????? 10 > USA??????????? y???????? 40 > USA??????????? z???????? 70 > UK???????????? x???????? 50 > > UK???????????? y???????? 80 > UK???????????? z???????? 62 > Canada???????? x???????? 20 > Canada???????? y???????? 35 > Canada???????? z???????? 10 > > I did try reshape package but things are in mess. Please guide > > Regards > > Vincy > > > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at 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. > >
Try this: reshape(xx, direction = "long", varying = list(2:4), idvar = "country") On Wed, Mar 9, 2011 at 11:15 AM, Vincy Pyne <vincy_pyne@yahoo.ca> wrote:> Dear R helpers, > > xx = data.frame(country = c("USA", "UK", "Canada"), x = c(10, 50, 20), y > c(40, 80, 35), z = c(70, 62, 10)) > > > xx > country x y z > 1 USA 10 40 70 > 2 > UK 50 80 62 > 3 Canada 20 35 10 > > > > > I need to arrange this as a new data.frame as follows - > > country type values > USA x 10 > USA y 40 > USA z 70 > UK x 50 > > UK y 80 > UK z 62 > Canada x 20 > Canada y 35 > Canada z 10 > > I did try reshape package but things are in mess. Please guide > > Regards > > Vincy > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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]]
Isn't reshape sun at first :).
The not obvious thing is that you don't need to cast anything. All you need
is the proper melt command (and a final sort if you want the table you have).
====================================================================library(reshape)
xx = data.frame(country = c("USA", "UK",
"Canada"), x = c(10, 50, 20),
y = c(40, 80, 35), z = c(70, 62, 10))
yy <- melt(xx, id = c("country"), measured =
c("x","y",,"z"))
yy[order(yy$country, decreasing = TRUE),]
====================================================================--- On Wed,
3/9/11, Vincy Pyne <vincy_pyne at yahoo.ca> wrote:
> From: Vincy Pyne <vincy_pyne at yahoo.ca>
> Subject: [R] Rearranging the data
> To: r-help at r-project.org
> Received: Wednesday, March 9, 2011, 9:15 AM
> Dear R helpers,
>
> xx = data.frame(country = c("USA", "UK",
"Canada"), x > c(10, 50, 20), y = c(40, 80, 35), z = c(70, 62, 10))
>
> > xx
> ? ? ?? country????? x???? y??? z
> 1????? USA??????? 10??? 40? 70
> 2?????
> UK????????? 50?? 80?? 62
> 3???? Canada??? 20?? 35?? 10
>
>
>
>
> I need to arrange this as a new data.frame as follows -
>
> country?????? type???? values
> USA??????????? x???????? 10
> USA??????????? y???????? 40
> USA??????????? z???????? 70
> UK???????????? x???????? 50
>
> UK???????????? y???????? 80
> UK???????????? z???????? 62 ?
> Canada???????? x???????? 20 ?
> Canada???????? y???????? 35
> Canada???????? z???????? 10
>
> I did try reshape package but things are in mess. Please
> guide
>
> Regards
>
> Vincy ?
> ?
>
>
> ??? [[alternative HTML version deleted]]
>
>
> -----Inline Attachment Follows-----
>
> ______________________________________________
> R-help at 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.
>