[R] order a data frame after date and hour

Dennis Murphy djmuser at gmail.com
Fri Jul 29 07:22:03 CEST 2011


Hi:

On Thu, Jul 28, 2011 at 1:05 AM, anglor <angelica.ekenstam at dpes.gu.se> wrote:
> Hi,
>
> I've got a dataframe looking like this:
>
>                DateHour TcuvInt.A TcuvInt.B TcuvInt.C
> 1757 2007-03-15 14:00:00                7.83      <NA>
> 1758 2007-03-15 14:30:00      7.42      7.69      <NA>
> 1759 2007-03-15 15:00:00      7.53      7.75      <NA>
> 1760 2007-03-15 15:30:00      7.65      7.73      <NA>
>
>
> and I need to sort it after the DateHour variable. It is mostly sorted
> already except that in the end there is X dates that should be in the
> beginning. I tried to find a way to convert the datehour to a number, looked
> at format but it didn't work for me.

See ?DateTimeClasses for an overview of the various date/time classes
available in R. I'd suggest looking into as.POSIXct() and strptime()
for starters. Make sure you try several examples. If your library has
Phil Spector's book 'Data Manipulation with R', there is a very nice
chapter on dealing with date-time variables.
>
> Any suggestions?
>
> And also, I'm quite new at R and this forum, someone suggested I use
>
> df <- data.frame(x = 1:4, y = rnorm(4), z = rpois(4, 3))
> structure(list(x = 1:4, y = c(-0.24950486967999, 0.151728291232845,
> 1.24654200540763, 2.30868457813145), z = c(3, 5, 2, 5)), .Names = c("x",
> "y", "z"), row.names = c(NA, -4L), class = "data.frame")
> dput(df)
>
> for a better display of my problem but I don't understand what this does?

What it does is transfer the content and structure of your data into a
text form that people can copy and paste directly into their R
sessions, with the confidence that the data they're looking at is the
same as what you see. This matters especially for date/time, character
or factor data, where copying and pasting the contents of your R
console into an e-mail can well lose the structure of those types of
objects in translation.

For the example you gave, I can copy and paste the dput() output into
my session to get

df <- structure(list(x = 1:4, y = c(-0.24950486967999, 0.151728291232845,
 1.24654200540763, 2.30868457813145), z = c(3, 5, 2, 5)), .Names = c("x",
 "y", "z"), row.names = c(NA, -4L), class = "data.frame")
df
  x          y z
1 1 -0.2495049 3
2 2  0.1517283 5
3 3  1.2465420 2
4 4  2.3086846 5
str(df)
'data.frame':   4 obs. of  3 variables:
 $ x: int  1 2 3 4
 $ y: num  -0.25 0.152 1.247 2.309
 $ z: num  3 5 2 5

which should be exactly what you see if you type the above code into
your R console. This is what is meant by a 'reproducible example' (at
least the data side of it...)

Unfortunately, I don't know whether the DateHour variable you copied
and pasted into your e-mail is character, factor or one of several
possible date-time classes. Had you used dput() on that data, you
certainly would have had a satisfactory answer by now. Justin gave the
most probable solution, but even he was uncertain about the class of
the DateHour variable, with good reason.

HTH,
Dennis
>
> Thank you for help!
>
> Angelica
>
> --
> View this message in context: http://r.789695.n4.nabble.com/order-a-data-frame-after-date-and-hour-tp3700721p3700721.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list