[R] How to order or sort a data.frame
John Kane
jrkrideau at yahoo.ca
Mon Aug 14 22:10:30 CEST 2006
--- Dimitrios Rizopoulos
<Dimitris.Rizopoulos at med.kuleuven.be> wrote:
> Quoting John Kane <jrkrideau at yahoo.ca>:
>
> >
> > --- Dimitrios Rizopoulos
> > <Dimitris.Rizopoulos at med.kuleuven.be> wrote:
> >
> >> try the following:
> >>
> >> mdf <- data.frame(us.state, count, year, month)
> >> mdf[order(mdf$year, mdf$month), ]
> >>
> >
> >
> > Thansk to Dimitris and Dieter. This has helped
> since
> > seems to have shown me a way around the problem.
> It
> > just means that I have to sort the data earlier.
> >
> > What my example did not show clearly is that when
> I
> > subset the data the variable 'month' which is
> numeric
> > in the original dataframe becomes a factor. I was
> > wondering if there was a way to sort the factor so
> > that I would get a numeric sort. So far I have
> not
> > been able to see how to coerce the factor "month"
> into
> > a numeric when using 'order"
>
> for converting factors to numeric check R FAQ 7.10.
>
> Best,
> Dimitris
Argh! Of course. I should never trust my memory!
Thanks.
>
>
> >
> > Thanks for helpl.
> >
> > Hi Hadley,
> > I have not had time to check out the reshape but
> > thanks.
> >
> >
> >
> >
> >> 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
> >>
> >>
> >
> > ______________________________________________
> > 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