Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut function in R

Tags:

r

My dataset class has "time" feature. This feature belongs to character class. I try to show the frequency of pick up per different time slot.For this reason, I used "cut" function as below:

FreqPickupTime <- cut(dt$time, breaks = "hour")

But, I occur with the below error.

Error in cut.default(dt$time, breaks = "hour") :'x' must be numeric.

is there any solution to use this cut function for character features.

like image 289
Behzad Nazarbakhsh Avatar asked Jan 31 '26 07:01

Behzad Nazarbakhsh


1 Answers

As MrFlick says, cut() won't cut it for characters.

Say df$time looks something like: 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 could simply do:

table(substr(time, 1, regexpr(':', time)-1))
like image 92
Majo Avatar answered Feb 01 '26 23:02

Majo



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!