Cut function in R
My dataset class has a function < time . This function belongs to the character class. I am trying to show the pickup frequency for a different time slot. For this reason, I used the "cut" function as shown below:
FreqPickupTime <- cut(dt$time, breaks = "hour")
But I am encountering below error.
Error in cut.default (dt $ time, breaks = "hour"): 'x' must be numeric.
is there any solution to use this cut function for feature functions.
+3
source to share
1 answer
As MrFlick says, cut()
won't cut it for characters.
Say df$time
it looks something like this:16:42, 12:32, 03:20...
For example:
time <- paste0(round(runif(1000, 0, 23), digits = 0), ':', round(runif(1000, 1, 59), digits = 0))
You can simply do:
table(substr(time, 1, regexpr(':', time)-1))
+2
source to share