[R] Data reshaping

Duncan Mackay mackay at northnet.com.au
Tue May 28 05:25:18 CEST 2013


library(reshape2)

dcast(Dat, X1 ~X2, value.var = "X3")
   X1  1  2  3  4
1  A 11 12 13 14
2  B 15 16 17 18
3  C 19 20 21 NA

or  use ? reshape

HTH

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au



At 10:37 28/05/2013, you wrote:
>Hello again, let say I have following data-frame:
>
> > Dat <- data.frame(c(rep(c("A", "B"), each = 4), "C", "C", "C"),
>c(rep(1:4, 2), 1, 2, 3), 11:21)
> > colnames(Dat) <- c("X1", "X2", "X3")
> > Dat
>    X1 X2 X3
>1   A  1 11
>2   A  2 12
>3   A  3 13
>4   A  4 14
>5   B  1 15
>6   B  2 16
>7   B  3 17
>8   B  4 18
>9   C  1 19
>10  C  2 20
>11  C  3 21
>
>
>Now I want to put that data-frame in the following form:
>
> > Dat1 <- rbind(c(11,12,13,14), c(15,16,17,18), c(19,20,21, NA));
>colnames(Dat1) <- c(1,2,3,4); rownames(Dat1) <- c("A", "B", "C")
> > Dat1
>    1  2  3  4
>A 11 12 13 14
>B 15 16 17 18
>C 19 20 21 NA
>
>
>Basically, 'Dat' is the melted form of 'Dat1'
>
>Can somebody point me any R function for doing that?
>
>Thanks for your help.
>
>         [[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.



More information about the R-help mailing list