2

I am trying to create a vector indicating the end of a sequence.

My data looks this :

   id time   var wake
1   1    1 sleep    0
2   1    2 sleep    0
3   1    3 sleep    0
4   1    4     0    0
5   1    5     0    0

What I want is this (output wanted)

   id time   var wake
1   1    1 sleep    0
2   1    2 sleep    0
3   1    3 sleep    0
4   1    4     0    1
5   1    5     0    0
6   1    6     0    0
7   1    7     0    0
8   1    8 sleep    0
9   1    9 sleep    0
10  1   10 sleep    0
11  2    1 sleep    0
12  2    2 sleep    0
13  2    3 sleep    0
14  2    4 sleep    0
15  2    5 sleep    0
16  2    6     0    1
17  2    7     0    0
18  2    8     0    0
19  2    9 sleep    0
20  2   10 sleep    0

I was thinking of something like

library(dplyr) 

dt$time = as.numeric(as.character(dt$time))
dt$var = ifelse(dt$var == 'sleep', 1, 0)

dt = dt %>% group_by(id) %>% 
mutate(grp = cumsum(var != lag(var, default = var[1])))

dt$wake = 0
dt$wake [dt$grp == 1] <- 1

However, it doesn't spot the first episode only

data

dt = structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", 
"2"), class = "factor"), time = structure(c(1L, 3L, 4L, 5L, 6L, 
 7L, 8L, 9L, 10L, 2L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 2L), .Label =     c("1", 
"10", "2", "3", "4", "5", "6", "7", "8", "9"), class = "factor"), 
var = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L), .Label = c("0", 
"sleep"), class = "factor")), .Names = c("id", "time", "var"
), row.names = c(NA, -20L), class = "data.frame")
Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38
giac
  • 4,261
  • 5
  • 30
  • 59
  • Doesn't somethig like `diff(rleid(dt$var))` be ok ? (using `rleid` from `data.table`) – Tensibai Nov 28 '16 at 14:03
  • Can you clarify that if an `id` has `var = c("sleep", "sleep", 0, 0, "sleep", "sleep", 0, 0)` then do you want to mark all wakings as in `wake = c(0, 0, 1, 0, 0, 0, 1, 0)` or just the first as in `wake = c(0, 0, 1, 0, 0, 0, 0, 0)` – G. Grothendieck Nov 28 '16 at 15:58

5 Answers5

4

In one pass with library data.table:

setDT(dt)
dt[,wake:=( c(0,diff( rleid(var) ) == 1) & var != "sleep"),by=id]

The idea is to get the run length encoding of var (rleid):

> dt[,rleid(var),by=id][,V1]
[1] 1 1 1 2 2 2 2 3 3 3 1 1 1 1 1 2 2 2 3 3

And it's diff +1 when going from sleep to 0, or 0 to sleep, negative when changing group (start again at 1):

> diff(dt[,rleid(var),by=id][,V1])
[1]  0  0  1  0  0  0  1  0  0 -2  0  0  0  0  1  0  0  1  0

And where it is 1 and var is not sleep get a TRUE value (could be 1 if you wrap the whole thing into as.numeric).

Output:

    nrow id time   var  wake
 1:    1  1    1 sleep FALSE
 2:    2  1    2 sleep FALSE
 3:    3  1    3 sleep FALSE
 4:    4  1    4     0  TRUE
 5:    5  1    5     0 FALSE
 6:    6  1    6     0 FALSE
 7:    7  1    7     0 FALSE
 8:    8  1    8 sleep FALSE
 9:    9  1    9 sleep FALSE
10:   10  1   10 sleep FALSE
11:   11  2    1 sleep FALSE
12:   12  2    2 sleep FALSE
13:   13  2    3 sleep FALSE
14:   14  2    4 sleep FALSE
15:   15  2    5 sleep FALSE
16:   16  2    6     0  TRUE
17:   17  2    7     0 FALSE
18:   18  2    8     0 FALSE
19:   19  2    9 sleep FALSE
20:   20  2   10 sleep FALSE
Tensibai
  • 15,557
  • 1
  • 37
  • 57
  • thanks. Sorry I'm not very familiar with `data.table`. How is the `id` grouped here ? – giac Nov 28 '16 at 14:24
  • Aww, sorry, I didn't grouped by id. I missed this part in your question. I added the `by=id`which would compute the rle by id :p – Tensibai Nov 28 '16 at 14:36
3

The following should work with dplyr:

library(dplyr)
dt <- dt %>% group_by(id) %>%
             mutate(wake = as.integer(var == '0' & var != lag(var, default = var[1])))
##Source: local data frame [20 x 4]
##Groups: id [2]
##
##       id   time    var  wake
##   <fctr> <fctr> <fctr> <dbl>
##1       1      1  sleep     0
##2       1      2  sleep     0
##3       1      3  sleep     0
##4       1      4      0     1
##5       1      5      0     0
##6       1      6      0     0
##7       1      7      0     0
##8       1      8  sleep     0
##9       1      9  sleep     0
##10      1     10  sleep     0
##11      2      1  sleep     0
##12      2      2  sleep     0
##13      2      3  sleep     0
##14      2      4  sleep     0
##15      2      5  sleep     0
##16      2      6      0     1
##17      2      7      0     0
##18      2      8      0     0
##19      2      9  sleep     0
##20      2     10  sleep     0

Compare var with lag of var as the OP did to detect the transition (or episode) between sleep and 0 but add the condition that var needs to be 0 to mark only those transitions from sleep to 0 for wake to be 1.

aichao
  • 7,375
  • 3
  • 16
  • 18
3

Assuming that you want to mark all awakings for each id:

1) no packages Note that if x and y are logical then x - y > 0 iff x is TRUE and y is FALSE. Thus we have the following that uses no packages:

transform(dt, wake = ave(var == 0, id, FUN = function(x) c(0, diff(x) > 0)))

2) dplyr Alternately it can be written in terms of dplyr like this:

library(dplyr)
dt %>% group_by(id) %>% mutate(wake = c(0, diff(var == 0) > 0)) %>% ungroup()

3) zoo We can use use rollapplyr along each id looking for the sequence c(FALSE, TRUE) in var==0. Adding 0 ensures that the result is numeric.

library(zoo)
roll <- function(x) rollapplyr(x, 2, identical, c(FALSE, TRUE), fill = 0)
transform(dt, wake = ave(var == 0, id, FUN = roll) + 0)

Note: If you wanted to mark only the first awakening for each id and if out is the result of any of the above then:

transform(out, wake = ave(wake, id, FUN = function(x) replace(0*x, which.max(x), max(x))))
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
2

An ugly way of doing this could be by substract the i:th +1 with the i:th value to see if the sleep ended, like this:

vec<-ifelse(dt$var=="sleep",1,0) #creating a vector for sleeping/not sleeping
tg<-c(0,vec[1:(length(vec)-1)])-vec #if values are == 1, i.e. first episode of not sleeping

then you can just write following code to get wake:

ifelse(tg==1,1,0)  [1] 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

EDIT: thanks to the comments below I updated my answer.

vec<-ifelse(dt$var=="sleep",1,0)

lapply(unique(dt$id), function(x) ifelse(c(0,vec[min(which(dt$id==x)):(max(which(dt$id==x))-1)])-vec[dt$id==x]==1,1,0))
nadizan
  • 1,323
  • 10
  • 23
1

Not very elegant, but this does the job. Make sure you sort the dt by id and time beforehand.

dt$id <- as.character(dt$id)
dt$time <- as.integer(as.character(dt$time))
dt$var <- as.character(dt$var)
dt <- dplyr::arrange(dt, id, time)
dt$wake <- 0
dt$wake[which(dt$var == "0" & lag(dt$var) == "sleep" & 
                dt$id == lag(dt$id))] <- 1
Kota Mori
  • 6,510
  • 1
  • 21
  • 25