[R] How to order or sort a data.frame

Dimitrios Rizopoulos Dimitris.Rizopoulos at med.kuleuven.be
Sun Aug 13 17:03:28 CEST 2006


try the following:

mdf <- data.frame(us.state, count, year, month)
mdf[order(mdf$year, mdf$month), ]


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting John Kane <jrkrideau at yahoo.ca>:

> 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.
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
>



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list