Reputation: 481
I have a column 'Incident_Time' representing time in a data frame in R.
when I call the str() function on the column it says something like this
str(crime_data$Incident_Time)
Factor w/ 1439 levels "00:01:00","00:02:00",..: 840 945 1140 981 1260 969 1020 840 980 765 ...
I want to convert this column into string type such that if the time is less than 12:00:00, it should get modified to the string "morning" , if time is between 12:00:00 and 6:00:00 , it is "daylight" and so on.
I typed the following commands:
time.tag <- chron(times=c("00:00:00", "06:00:00", "12:00:00", "18:00:00", "23:59:59"))
labels <- c("Early Morning", "Morning", "Evening", "Night")
crime_data$Incident_Time_Range <- cut(crime_data$Incident_Time, breaks = time.tag, labels, include.lowest = TRUE )
Then the following error occurred:
Error in cut.default(crime_data$Incident_Time, breaks = time.tag, labels, : 'x' must be numeric
Please help me to identify the problem. Thank You.
Upvotes: 0
Views: 1740
Reputation: 1932
To provide a complete example.
Example Data
sampletime<-chron(times=as.character(as.ITime(Sys.time()+sample(86400,100))))
No modifications needed to code but it would not work until I converted sampletime
to class times
by using chron
. I believe cut will not work if your data is not the same class
.
cut(sampletime, breaks = time.tag, labels, include.lowest = TRUE )
[1] Early Morning Evening Evening Evening
[5] Morning Night Evening Night
[9] Night Morning Early Morning Early Morning
[13] Evening Early Morning Morning Early Morning
[17] Evening Morning Early Morning Evening
....
Upvotes: 0
Reputation: 1233
Unverified as there is no sample data, but you shouldn't have your incident time as a factor. Convert to time:
crime_data$Incident_Time <- chron(times=as.character(crime_data$Incident_Time))
Then run your existing code
Upvotes: 1