Hi Friends, I'm new to R.I have a data frame : xxx having columns color name values R XXX 10 G YYY 4 Y ZZZ 5 G XXX 2 Y XXX 3 R YYY 2 Y YYY 1 R ZZZ 2 G ZZZ 3 Now i want to have the data in this format R G Y XX X 10 2 3 YYY 2 4 1 ZZZ 2 3 5 Can you please help me on this. Thanks, Namit -- View this message in context: http://r.789695.n4.nabble.com/creating-Pivot-tp4637711.html Sent from the R help mailing list archive at Nabble.com.
On 07/25/2012 02:41 PM, namit wrote:> Hi Friends, > > > I'm new to R.I have a data frame : > > xxx having columns color name values > R XXX 10 > G YYY 4 > Y ZZZ 5 > G XXX 2 > Y XXX 3 > R YYY 2 > Y YYY 1 > R ZZZ 2 > G ZZZ 3 > > > Now i want to have the data in this format R G Y > > > XX X 10 2 3 > > YYY 2 4 1 > > ZZZ 2 3 5 > > Can you please help me on this. >Hi Namit, I think what you want is "reshape". Try this: ndat<-data.frame(col=c("R","G","Y","G","Y","R","Y","R","G"), xyz=c("XXX","YYY","ZZZ","XXX","XXX","YYY","YYY","ZZZ","ZZZ"), val=c(10,4,5,2,3,2,1,2,3)) reshape(ndat,v.names="val",idvar="xyz",timevar="col",direction="wide") Jim
Hello, Try the following. dat <- read.table(text=" color name values R XXX 10 G YYY 4 Y ZZZ 5 G XXX 2 Y XXX 3 R YYY 2 Y YYY 1 R ZZZ 2 G ZZZ 3 ", header=TRUE) d xtabs(values ~ name + color, data=dat) Hope this helps, Rui Barradas Em 25-07-2012 05:41, namit escreveu:> Hi Friends, > > > I'm new to R.I have a data frame : > > xxx having columns color name values > R XXX 10 > G YYY 4 > Y ZZZ 5 > G XXX 2 > Y XXX 3 > R YYY 2 > Y YYY 1 > R ZZZ 2 > G ZZZ 3 > > > Now i want to have the data in this format R G Y > > > XX X 10 2 3 > > YYY 2 4 1 > > ZZZ 2 3 5 > > Can you please help me on this. > > > Thanks, > Namit > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/creating-Pivot-tp4637711.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
# note file name is xx. library(reshape2) dcast(xx, name ~ color, value.var = "values") John Kane Kingston ON Canada> -----Original Message----- > From: saileshchowdary at gmail.com > Sent: Tue, 24 Jul 2012 21:41:19 -0700 (PDT) > To: r-help at r-project.org > Subject: [R] creating Pivot > > Hi Friends, > > > I'm new to R.I have a data frame : > > xxx having columns color name values > R XXX 10 > G YYY 4 > Y ZZZ 5 > G XXX 2 > Y XXX 3 > R YYY 2 > Y YYY 1 > R ZZZ 2 > G ZZZ 3 > > > Now i want to have the data in this format R G Y > > > XX X 10 2 3 > > YYY 2 4 1 > > ZZZ 2 3 5 > > Can you please help me on this. > > > Thanks, > Namit > > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/creating-Pivot-tp4637711.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!