简体   繁体   中英

segregate data and assign new value R

I have a column for Time which has data range between 0000 - 2399 I have 2.5 million rows and each cell has different values for time.

I wish to segregate my data into 4

Night 0000:0700
Morning 0701:1200
Afternoon 1201:1800
Evening 1801:2359

So if the time range is between 0000:0700 assign the value of 1 for all values within the new column.

mydata$NEWTIME <- as.nemeric(factor(mydata$TIME,
   level=range(0000:0700,0701:1200,1201:1800,1801:2359))

this is not working and I am sure I am doing something silly. Could you please help me?

This should work:

mydata$TIME[mydata$TIME>0000 & mydata$TIME<0700]<-"Night"
mydata$TIME[mydata$TIME>0701 & mydata$TIME<1200]<-"Morning"
mydata$TIME[mydata$TIME>1201 & mydata$TIME<1800]<-"Afternoon"
mydata$TIME[mydata$TIME>1801 & mydata$TIME<2359]<-"Evening"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM