Reputation: 1599
I've a time column that's of type character. I want to convert it to some sort of time type. I like to use the lubridate
package but I don't like the notation of the output. My goal is to later group the date per hour and count the cases. df %>% group_by(floor_date(Tijd, "hour"))
.
Dataframe:
df <- structure(list(Tijd = c("01:00:00", "01:27:00", "09:25:00", "14:39:00",
"15:35:00")), .Names = "Tijd", row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
The following code produces a a type where the floor_date()
function doesn't work on.
library(tidyverse)
library(lubridate)
df %>%
mutate(Tijd = hms(Tijd))
Output:
# A tibble: 5 x 1
Tijd
<S4: Period>
1 1H 0M 0S
2 1H 27M 0S
3 9H 25M 0S
4 14H 39M 0S
5 15H 35M 0S
The following code gives an error:
df %>%
mutate(Tijd = hms(Tijd)) %>%
group_by(floor_date(Tijd, "hour"))
Error in mutate_impl(.data, dots) :
Evaluation error: 'origin' must be supplied.
Upvotes: 2
Views: 3048
Reputation: 11955
In case you are interested in chron
approach then
library(dplyr)
library(chron)
df %>%
mutate(Tijd = hours(times(Tijd)))
gives
Tijd
<dbl>
1 1.00
2 1.00
3 9.00
4 14.0
5 15.0
Sample data:
df <- structure(list(Tijd = c("01:00:00", "01:27:00", "09:25:00", "14:39:00",
"15:35:00")), .Names = "Tijd", row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
Upvotes: 0
Reputation: 348
One of the way -
hr <- strptime(df$Tijd, format = '%H:%M:%S', 'GMT')
df$hr <- hour(hr)
Upvotes: 2