I have a dataframe where I would like to order first
by  variable, year, and then within that variable by
month.

So far the only way that I have seen to do this is to
order by year and then subset year and sort by month
and then do an rbind to get things back together.  

Is this the right approach?  

Example:

us.state <-rep("California", 23)                      
                         
count <- c(774,283,774,283,508,283,774,283,602,283,   
                         
774,508,0,602,330,283,283,283,602,301,126, NA,301)    
                         
year <- c(2002,  2003, 2001, 2002, 2001, 2002, 2001,
2002, 2002, 2003,          
          2002, 2002, 2001,  2002, 2001, 2002, 2001,
2002, 2001, 2002,          
          2001, 2001, 2002)                           
                         
month <- c( 1, 1, 10, 10, 11, 11, 12, 12,             
                         
            2, 2, 3, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
9)                        
                                                      
                         
                                                      
                         
df <- data.frame (cbind(us.state,count, year, month)) 
                         
# ordering as a factor works here                     
                         
df1 <- df[order(df$year),]                            
                         
df1                                                   
                         
                                                      
                         
df2 <- subset(df1, year==2001)                        
                         
                                                      
                         
# ordering as a factor works but not a good
appearance.                         
                                                      
                         
df3 <- df2[order(as.numeric(df2$month)),]             
                         
df3                                                   
                         


This works but  "month" is ordered as  a factor and I
would prefer to coerce it into a numeric for
presentation purposes but 
 df3 <- df2[order(as.numeric(df2$month)),] does not
seem to work,  nor has a couple of other things I've
tried.  

Any suggestions gratefully received.

______________________________________________
[email protected] 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