0

I have two variables that represent the wake-up time and go to sleep time in minutes (example: If Person A went to bed at 00:40 it would be "40" for sleep_min).

  id      day wake_min sleep_min         
1 ADD15     1      518        40
2 ADD15     2      540        45
3 ADD15     3      570        80
4 ADD15     4      487        50
5 ADD15     5      582        73
6 AHK1      1      405      1435
7 AHK1      2      611      1402   

Then I have a second data set that has a row for each day and minute for every person. So Person ADD15 has 1440 rows for day 1, 1440 for day 2 etc.

      id day minute
1: ADD15   1      1
2: ADD15   1      2
3: ADD15   1      3
4: ADD15   1      4
5: ADD15   1      5
6: ADD15   1      6
...

I want to create a new variable named "state" for the second data set. This one should represent if a person is whether sleeping or awake. The variable should be coded 1 for sleeping and 0 for being awake. In the case of day 1 of person ADD15, all rows for the minutes between 40 and 518 should have the value "1" and the other rows for day 1 "0".

I tried some things with teh ifelse function but nothing worked so far.

I would really appreciate some help and advices for my problem!

Thanks in advance!!

psycho95
  • 131
  • 1
  • 12

2 Answers2

0

You could merge the datasets, then check if minute is in the sleep sequence.

dat3 <- merge(dat1, dat2)
dat3$asleep <-
  +mapply(`%in%`,
          dat3$minute,
          apply(dat3[3:4], 1, function(x) do.call(seq, as.list(unname(x))))
  )
head(dat3, 20)
#       id day wake_min sleep_min minute asleep
# 1  ADD15   1      518        40    264      1
# 2  ADD15   1      518        40    265      1
# 3  ADD15   1      518        40    262      1
# 4  ADD15   1      518        40    263      1
# 5  ADD15   1      518        40    272      1
# 6  ADD15   1      518        40    273      1
# 7  ADD15   1      518        40    274      1
# 8  ADD15   1      518        40    275      1
# 9  ADD15   1      518        40    276      1
# 10 ADD15   1      518        40    277      1
# 11 ADD15   1      518        40    278      1
# 12 ADD15   1      518        40    266      1
# 13 ADD15   1      518        40    267      1
# 14 ADD15   1      518        40    268      1
# 15 ADD15   1      518        40    269      1
# 16 ADD15   1      518        40    270      1
# 17 ADD15   1      518        40    271      1
# 18 ADD15   1      518        40      1      0
# 19 ADD15   1      518        40      2      0
# 20 ADD15   1      518        40      3      0

Data:

dat1 <- structure(list(id = c("ADD15", "ADD15", "ADD15", "ADD15", "ADD15", 
"AHK1", "AHK1"), day = c(1L, 2L, 3L, 4L, 5L, 1L, 2L), wake_min = c(518L, 
540L, 570L, 487L, 582L, 405L, 611L), sleep_min = c(40L, 45L, 
80L, 50L, 73L, 1435L, 1402L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7"))

dat2 <- expand.grid(id=unique(dat1$id), day=1:3, minute=1:1440)
jay.sf
  • 60,139
  • 8
  • 53
  • 110
0

Thank your very much for your quick answer!

This worked so far, but theres still one problem. If the person went to sleep after midnight, the function works perfectly. But if the person went to bed before midnight, it doesn't.

    id day wake_min sleep_min minute sleep
1  AHK1   1      405      1435    395 FALSE
2  AHK1   1      405      1435    396 FALSE
3  AHK1   1      405      1435    397 FALSE
4  AHK1   1      405      1435    398 FALSE
5  AHK1   1      405      1435    399 FALSE
6  AHK1   1      405      1435    400 FALSE
7  AHK1   1      405      1435    401 FALSE
8  AHK1   1      405      1435    402 FALSE
9  AHK1   1      405      1435    403 FALSE
10 AHK1   1      405      1435    404 FALSE
11 AHK1   1      405      1435    405  TRUE
12 AHK1   1      405      1435    406  TRUE

This person went to sleep at minute 1435 the day before. So for day 1 minute 0 to 405 should be TRUE. I don't know how to handle this.

psycho95
  • 131
  • 1
  • 12