mydf <- tibble::tribble(
~seq, ~flag,
0, 0,
0, 0,
0, 0,
1, 1,
1, 1,
7, 1,
1, 1,
3, 1,
2, 1,
1, 1,
1, 1,
0, 1,
0, 1,
0, 0,
0, 0,
1, 1,
1, 1,
7, 1,
1, 1,
3, 1,
2, 1,
1, 1,
1, 1,
0, 1,
0, 1,
0, 0,
0, 0,
2, 0
)
我有一个带有序列列的数据框。如何创建二进制标记来标记任何特定的数字序列?我的示例序列是1,1,7,1,3,2,1,1,0,0。
答案 0 :(得分:2)
library(zoo)
match_seq <- c(1, 1, 7, 1, 3, 2, 1, 1, 0, 0)
is_start <- rollapply(mydf$seq, length(match_seq), function(x) all(x == match_seq))
inds <- sapply(which(is_start), `+`, seq_along(match_seq) - 1)
mydf$flag2 <- as.numeric(1:nrow(mydf) %in% inds)
# seq flag flag2
# 1 0 0 0
# 2 0 0 0
# 3 0 0 0
# 4 1 1 1
# 5 1 1 1
# 6 7 1 1
# 7 1 1 1
# 8 3 1 1
# 9 2 1 1
# 10 1 1 1
# 11 1 1 1
# 12 0 1 1
# 13 0 1 1
# 14 0 0 0
# 15 0 0 0
# 16 1 1 1
# 17 1 1 1
# 18 7 1 1
# 19 1 1 1
# 20 3 1 1
# 21 2 1 1
# 22 1 1 1
# 23 1 1 1
# 24 0 1 1
# 25 0 1 1
# 26 0 0 0
# 27 0 0 0
# 28 2 0 0
在没有is_start
的情况下计算library(zoo)
的一种方法是
subseqs <-
sapply(seq(0, nrow(mydf) - length(match_seq)),
function(i) mydf$seq[i + seq_along(match_seq)])
is_start <- colMeans(subseqs == match_seq) == 1
答案 1 :(得分:1)
使用tidyverse
的解决方案。
library(tidyverse)
st <- str_c(mydf$seq, collapse = "")
pos <- str_locate_all(st, "1171321100")
index <- map2(pos[[1]][, 1], pos[[1]][, 2], `:`) %>% unlist()
mydf2 <- mydf %>%
mutate(Result = as.integer(row_number() %in% index))
mydf2
# # A tibble: 28 x 3
# seq flag Result
# <dbl> <dbl> <int>
# 1 0 0 0
# 2 0 0 0
# 3 0 0 0
# 4 1 1 1
# 5 1 1 1
# 6 7 1 1
# 7 1 1 1
# 8 3 1 1
# 9 2 1 1
# 10 1 1 1
# # ... with 18 more rows