[R] help required state wise count

Jim Lemon jim at bitwrit.com.au
Tue May 25 11:40:41 CEST 2010


On 05/24/2010 10:21 PM, Mohan L wrote:
> ...
> in this case the state column becomes the row index. I want the state
> name as the first column. There may be a way to do it.
>
Hi Mohan,
You can do it with this awful kludge, but there must be a better way:

dummy<-data.frame(
  State=sample(c("NSW","NT","QLD","SA","TAS","WA"),100,TRUE),
  Month=sample(c("Jan","Feb","Mar","Apr","May","Jun",
   "Jul","Aug","Sep","Oct","Nov","Dec"),100,TRUE))
dummytab<-table(dummy$State,dummy$Month)
dfdummytab<-data.frame(State=rownames(dummytab),
  Jan=dummytab[,5],Feb=dummytab[,4],Mar=dummytab[,8],
  Apr=dummytab[,1],May=dummytab[,9],Jun=dummytab[,7],
  Jul=dummytab[,6],Aug=dummytab[,2],Sep=dummytab[,12],
  Oct=dummytab[,11],Nov=dummytab[,10],Dec=dummytab[,3])
rownames(dfdummytab)<-NULL

Jim



More information about the R-help mailing list