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+s789695n4649893...@n4.nabble.com>
To: eric <ericst...@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]]

______________________________________________
R-help@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.

Reply via email to