Seems like this should be easy but I'm struggling a bit. How do I rearrange a data frame to go from the first one to the second shown below ? State Date lbs TX 200701 400 TX 200702 650 TX 200703 950 TX 200704 1000 FL 200701 200 FL 200702 300 FL 200703 500 FL 200704 333 NJ 200701 409 NJ 200702 308 NJ 200703 300 NJ 200704 800 Date TX FL NJ 200701 400 200 409 200702 650 300 308 200703 950 500 300 200704 1000 333 800 -- View this message in context: http://r.789695.n4.nabble.com/Reshaping-a-dataframe-tp4649874.html Sent from the R help mailing list archive at Nabble.com.
What am I doing wrong ? Using the method you proposed on SLIGHTLY different data.> head(x)Region YearMon kg AP 200701 290,311 AP 200702 322,671 AP 200703 216,600 AP 200704 450,711 AP 200705 245,215 AP 200706 212,492 j <-dcast(x,YearMon~Region,value.var='kg') sing kg as value column: use value_var to override. rror in names(data) <- array_names(res$labels[[2]]) : 'names' attribute [4] must be the same length as the vector [1] j <-dcast(x,YearMon~Region,value_var='kg') rror in names(data) <- array_names(res$labels[[2]]) : 'names' attribute [4] must be the same length as the vector [1] -----Original Message----- From: arun kirshna [via R] <ml-node+s789695n4649889h70@n4.nabble.com> To: eric <ericstrom@aol.com> Sent: Sat, Nov 17, 2012 5:23 pm Subject: Re: Reshaping a dataframe HI, Try this: dat1<-read.table(text=" State Date lbs TX 200701 400 TX 200702 650 TX 200703 950 TX 200704 1000 FL 200701 200 FL 200702 300 FL 200703 500 FL 200704 333 NJ 200701 409 NJ 200702 308 NJ 200703 300 NJ 200704 800 ",sep="",header=TRUE,stringsAsFactors=FALSE) library(reshape2) dcast(dat1,Date~State,value.var="lbs") # Date FL NJ TX #1 200701 200 409 400 #2 200702 300 308 650 #3 200703 500 300 950 #4 200704 333 800 1000 A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Reshaping-a-dataframe-tp4649874p4649889.html This email was sent by arun kirshna (via Nabble) To receive all replies by email, subscribe to this discussion -- View this message in context: http://r.789695.n4.nabble.com/Reshaping-a-dataframe-tp4649874p4649890.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Didn't try the other two methods as I spent a bit of time trying to learn about reshape2. I was also able to melt the data and that went fine (although based on your post, melting is not needed). Any ideas on why reshape2 dcast is giving fits ? -- View this message in context: http://r.789695.n4.nabble.com/Reshaping-a-dataframe-tp4649874p4649892.html Sent from the R help mailing list archive at Nabble.com.
Could it be that my data is showing up as factors ?> class(x)1] "data.frame" str(x) data.frame': 284 obs. of 3 variables: $ Region : Factor w/ 4 levels "AP","EU","LA",..: 1 1 1 1 1 1 1 1 1 1 ... $ YearMon: int 200701 200702 200703 200704 200705 200706 200707 200708 200709 200710 ... $ kg : Factor w/ 284 levels "-18,646","-3,199,893",..: 123 137 97 175 107 96 173 178 121 146 .. -----Original Message----- From: arun kirshna [via R] <ml-node+s789695n4649893h94@n4.nabble.com> To: eric <ericstrom@aol.com> Sent: Sat, Nov 17, 2012 5:47 pm Subject: Re: Reshaping a dataframe HI, This is what I got: dat2<-read.table(text=" Region YearMon kg 1 AP 200701 290,311 2 AP 200702 322,671 3 AP 200703 216,600 4 AP 200704 450,711 5 AP 200705 245,215 6 AP 200706 212,492 ",sep="",header=TRUE,stringsAsFactors=FALSE) dcast(dat2,YearMon~Region,value.var="kg") # YearMon AP #1 200701 290,311 #2 200702 322,671 #3 200703 216,600 #4 200704 450,711 #5 200705 245,215 #6 200706 212,492 reshape(dat2,v.names="kg",idvar="YearMon",timevar="Region",direction="wide") # YearMon kg.AP #1 200701 290,311 #2 200702 322,671 #3 200703 216,600 #4 200704 450,711 #5 200705 245,215 #6 200706 212,492 #With xtabs(), this will not work because you need to replace the "commas" in kg column and change it to numeric dat2$kg<-as.numeric(gsub("[,]","",dat2$kg)) xtabs(kg~YearMon+Region,data=dat2) # Region #YearMon AP # 200701 290311 #200702 322671 #200703 216600 #200704 450711 #200705 245215 #200706 212492 A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Reshaping-a-dataframe-tp4649874p4649893.html This email was sent by arun kirshna (via Nabble) To receive all replies by email, subscribe to this discussion -- View this message in context: http://r.789695.n4.nabble.com/Reshaping-a-dataframe-tp4649874p4649894.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]