我正在嘗試創建一個指示序列結束的向量。查找第一個序列集
我的數據看起來這樣的:
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
我想是這樣的(輸出想要)
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
我喜歡
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
思維的東西但是,沒有發現第一集只有
數據
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")
不somethig像'差異( rleid(dt $ var))可以嗎? (使用'data.table'中的'rleid') – Tensibai
你能否澄清一下,如果一個'id'有var = c(「sleep」,「sleep」,0,0,「sleep」,「sleep」 0)'那麼你是否想要在wake = c(0,0,1,0,0,0,0,0)中標記所有wakes,或者只是在wake = c(0,0, 1,0,0,0,0,0)' –