[R] Data reshaping

arun smartpink111 at yahoo.com
Tue May 28 02:48:41 CEST 2013


res1<- xtabs(X3~X1+X2,data=Dat)
res1
#   X2
#X1   1  2  3  4
 # A 11 12 13 14
 # B 15 16 17 18
 # C 19 20 21  0
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
A.K.


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. 




More information about the R-help mailing list