Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

processing date and time data in R

Tags:

r

Dear all, I have a data frame which comes directly from a sensor. The data provide the date and time in a single column. I want R to be able to recognise this data and then create an adjacent column in the data frame which gives a number that corresponds to a new day in the time and date column. For example 25/02/2011 13:34 in data$time.date would give 1 in the new column data$day, and 26/02/2011 13:34 in data$time.date would get 2 and so on....

Does anyone know how to go about solving this? Thanks in advance for any help.

like image 953
ThallyHo Avatar asked Apr 21 '26 10:04

ThallyHo


1 Answers

You can use cut() and convert to numeric the factor resulting from that call. Here is an example with dummy data:

> sdate <- as.POSIXlt("25/02/2011 13:34", format = "%d/%m/%Y %H:%M")
> edate <- as.POSIXlt("02/03/2011 13:34", format = "%d/%m/%Y %H:%M")
> df <- data.frame(dt = seq(from = sdate, to = edate, by = "hours"))
> head(df)
                   dt
1 2011-02-25 13:34:00
2 2011-02-25 14:34:00
3 2011-02-25 15:34:00
4 2011-02-25 16:34:00
5 2011-02-25 17:34:00
6 2011-02-25 18:34:00

We cut the date time column into days using cut(). This results in a factor with the dates as labels. We convert this factor to numerics to get 1, 2, ...:

> df <- within(df, day <- cut(dt, "day", labels = FALSE))
> head(df, 13)
                    dt day
1  2011-02-25 13:34:00   1
2  2011-02-25 14:34:00   1
3  2011-02-25 15:34:00   1
4  2011-02-25 16:34:00   1
5  2011-02-25 17:34:00   1
6  2011-02-25 18:34:00   1
7  2011-02-25 19:34:00   1
8  2011-02-25 20:34:00   1
9  2011-02-25 21:34:00   1
10 2011-02-25 22:34:00   1
11 2011-02-25 23:34:00   1
12 2011-02-26 00:34:00   2
13 2011-02-26 01:34:00   2
like image 173
Gavin Simpson Avatar answered Apr 24 '26 01:04

Gavin Simpson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!