Reputation: 2833
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.
Upvotes: 2
Views: 3918
Reputation: 174778
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
Upvotes: 5
Reputation: 10437
You can achieve this using cut.POSIXt. For example:
dat <- data.frame(datetimes = Sys.time() - seq(360000, 0, by=-3600))
dat$day <- cut(dat$datetimes, breaks="day", labels=FALSE)
Note that this assumes your date time column is correclty formated as a date-time class.
See ?DateTimeClasses
for details.
Upvotes: 4