Convert time object to categorical (morning, afternoon, evening, night) variable in R?

后端 未结 5 962
青春惊慌失措
青春惊慌失措 2020-12-19 20:38

I have this vector in a data frame of times in the format of hours:minutes that I want converted to categorical times of day:

    time <- c(\"15:03\", \"0         


        
5条回答
  •  甜味超标
    2020-12-19 21:13

    This one is similar to @Onyambu, only using plyr's mapvalues() and lubridate's hour():

    library(lubridate)
    library(plyr)
    df$timeofdat<-   mapvalues(hour(df$time),from=c(0:23),
      to=c(rep("night",times=5), rep("morning",times=6),rep("afternoon",times=5),rep("night", times=8)))
    

提交回复
热议问题