Greetings people who know R better than I do. For a class I am working with the add health dataset. There are 3 questions repeated twice that involve sleep patterns (what hour did you go to be, is that AM or PM, What minuet--these 3 questions are then repeated for what time someone wakes up). Using these code snippets I have created the 2 variables on a 24 hour clock
data$H4SP2T[data$H4SP2T %in% c(6, 8)] <- NA
data$H4SP2M[data$H4SP2M %in% c(96, 98)] <- NA
data$H4SP2H[data$H4SP2H %in% c(96, 98)] <- NA
data$ampm2 <- car::recode(data$H4SP2T, "1=0; 2=12")
data$ampm2[data$H4SP2H==12 & data$H4SP2T==2]<-0
data$sleep <- data$H4SP2H + data$ampm2 + data$H4SP2M/60
data$H4SP1T[data$H4SP1T %in% c(6, 8)] <- NA
data$H4SP1M[data$H4SP1M %in% c(96, 98)] <- NA
data$H4SP1H[data$H4SP1H %in% c(96, 98)] <- NA
data$ampm <- car::recode(data$H4SP1T, "1=0; 2=12")
data$ampm[data$H4SP1H==12 & data$H4SP1T==2]<-0
data$wakeup <- data$H4SP1H + data$ampm + data$H4SP1M/60
summary(data$sleep)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
1.00   12.00   22.00   17.44   23.00  107.63    1390  
summary(data$wakeup)
  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
  1.000   5.750   6.500   7.023   7.500  23.500    1404 
I am running into a few snags however, and am looking for how best to proceed. The first big issue I am having is trying to figure out how to mash these together to get a 3rd variable that simple tells me how many hours someone sleeps, simply adding or subtracting them will not work because of the cyclic nature of time. The smaller issue I was having also comes from times cyclic nature throwing off the median time when people go to bed (slightly messing with wake up time but not as much - someone going to bed after 2400 much more common that someone waking up at 0100), because someone who goes to bed at 0100 is has gone to bed 1 hour later than someone who went to bed at 2400, not 23 hours earlier.
 
    