Skip to content
Prev 266913 / 398502 Next

order a data frame after date and hour

Hi:
On Thu, Jul 28, 2011 at 1:05 AM, anglor <angelica.ekenstam at dpes.gu.se> wrote:
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.
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