[R] Reshaping a table

arun smartpink111 at yahoo.com
Mon Apr 8 14:40:32 CEST 2013


Hi,
Try this:
dat1<-read.table(text="
X      Y1    Y2

0.1  3      2

0.2  2      1
",sep="",header=TRUE)


 res<-do.call(rbind,lapply(split(dat1,seq_len(nrow(dat1))),function(x) {Y=rep(colnames(x)[-1],x[-1]); X=rep(x[,1],length(Y)); data.frame(X,Y,stringsAsFactors=FALSE)}))
 row.names(res)<- 1:nrow(res)
 res
#    X  Y
#1 0.1 Y1
#2 0.1 Y1
#3 0.1 Y1
#4 0.1 Y2
#5 0.1 Y2
#6 0.2 Y1
#7 0.2 Y1
#8 0.2 Y2
A.K.



----- Original Message -----
From: IOANNA <ii54250 at msn.com>
To: 'r-help-r-project.org' <r-help at r-project.org>
Cc: 
Sent: Monday, April 8, 2013 8:09 AM
Subject: [R] Reshaping a table

Hello all, 



I have data in the form of a table:



X      Y1    Y2

0.1   3       2

0.2   2       1



And I would like to transform in the form:



X     Y

0.1   Y1

0.1   Y1

0.1   Y1

0.1   Y2

0.1   Y2

0.2  Y1

0.2  Y1

0.2  Y2



Any ideas how?



Thanks in advance, 

IOanna








    [[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