[R] Sort by timestamp

Arien Lam a.lam at geo.uu.nl
Fri Mar 27 10:25:06 CET 2009


Good morning Johannes,

This might help. Try:

df <- data.frame(V1=as.factor(c('2008-10-14 09:10:00','2008-10-14
9:20:20','2008-10-14 08:45:00')),V2=runif(3))

df           # is a dataframe, just as yours

class(df$V1) # is a factor, just as yours. See ?factor
             # This will probably not be ordered
             # in a way you like.

df$V1 <- as.POSIXct(df$V1, tz='CET') # makes it a time. See ?POSIXct

class(df$V1) # is a POSIX time now

df2 <- df[do.call(order, df), ] # see ?order

df2          # sorted in a way you like


Cheers, Arien


On Thu, March 26, 2009 08:54, j.k wrote:
>
> #Good morning alltogheter. I'm using R for a short time to analyse
> TimeSeries
> and I have the following Problem:
> #I have a bunch of Time Series:
> #First of all I import them from a txt File
>
> data.input01 <-read.csv("./LD/20081030.txt", header = TRUE, sep = ";",
> quote="\"", dec=",", fill = TRUE, comment.char="")
> data.input02 <-read.csv("./LD/20090305.txt", header = TRUE, sep = ";",
> quote="\"", dec=",", fill = TRUE, comment.char="")
> data.input03 <-read.csv("./LD/20081114.txt", header = TRUE, sep = ";",
> quote="\"", dec=",", fill = TRUE, comment.char="")
> data.input04 <-read.csv("./LD/20081201.txt", header = TRUE, sep = ";",
> quote="\"", dec=",", fill = TRUE, comment.char="")
> data.input05 <-read.csv("./LD/20081219.txt", header = TRUE, sep = ";",
> quote="\"", dec=",", fill = TRUE, comment.char="")
> data.input06 <-read.csv("./LD/20090107.txt", header = TRUE, sep = ";",
> quote="\"", dec=",", fill = TRUE, comment.char="")
>
> #After the import they look like that:
>
>                   V1       V2
> 1  2008-10-14 08:45:00 92130.68
> 2  2008-10-14 08:50:00 94051.70
> 3  2008-10-14 08:55:00 97050.85
> 4  2008-10-14 09:00:00 81133.81
> 5  2008-10-14 09:05:00 70705.40
> 6  2008-10-14 09:10:00 75213.92
> 7  2008-10-14 09:15:00 90876.14
> 8  2008-10-14 09:20:00 85995.17
>
> #Next steps are to combine them with rbind and sort duplicates out
>
> data.troughput01 <-
> rbind(data.input03,data.input01,data.input04,data.input02,data.input05,data.input06)
> data.troughput02 <- unique(data.troughput01)
>
> #The Problem is that the dates are mixed and I want to sort/order them by
> the date and time.
> #The class of the Date/time is as followed:
> class(data.input01$V1)
> [1] "factor"
>
> # I've already tried sort and order but it didn't work
> #Are there any suggestions, how I can solve this issue??
>
> Thanks in advance
> Johannes
>
> --
> View this message in context:
> http://www.nabble.com/Sort-by-timestamp-tp22717322p22717322.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.
>


-- 
drs. H.A. (Arien) Lam (Ph.D. student)
Department of Physical Geography
Faculty of Geosciences
Utrecht University, The Netherlands




More information about the R-help mailing list