1

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.

  • we kinda need `data` (i.e. paste output of `dput(data)`. `data` is also not a great variable name even though R is gd at figuring things out. it'll come back to bite you someday. – hrbrmstr Oct 19 '17 at 01:20
  • Including a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in your question will increase your chances of getting an answer. – Samuel Oct 19 '17 at 01:26
  • This works much easier if you have a date and the time. Then you can combine them to do the actual calculation. – Reeza Oct 19 '17 at 02:06

1 Answers1

0

To solve both of your issues you need to have create a starting time for reference and then have all of your time values be derived as x amount of hours after the start time.

Since i cant see the data I cant tell you exactly how to do it but there are 2 general solutions. First (and the most recommended) is to store them using as.POSIXlt with wake times being the day after sleep times.

Second is you can add 24 to all your wake times. (and then while printing a simple if(wakeup > 24){wakeup <- wakeup - 24} to undo said change.

Note: Since your 3 questions dont explicitly check, you will need to make some assumptions and conditions for whether the subject went to sleep/woke up on the first or second day.

Jesse
  • 283
  • 1
  • 5
  • 14