问题
structure(list(group = c(NA, "A", "B", NA, "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", NA, NA, "B", "B", "A", "A", NA, NA, "B", "B", "B", NA, "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", NA, NA, "B", "B",
NA, "A"), seq_break = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE)), .Names = c("group",
"seq_break"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-50L))
In the data above I need to define a column that would contain run-length type id of group
column (like data.table::rleid
produces, but ignoring NA
). As you can observe, we've got also the column seq_break
which should end a sequence. And it usually does, as when group = NA
then seq_break = TRUE
. But sometimes seq_break = TRUE
and group is A
or B
- then, the sequence should be ended and new one started even if the next row refers to the same group. So for example for rows 25:26
we should have two different sequence id, even though both events refers to group B
. Generally, the expected output is shown below:
structure(list(group = c(NA, "A", "B", NA, "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", NA, NA, "B", "B", "A", "A", NA, NA, "B", "B", "B", NA, "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", NA, NA, "B", "B",
NA, "A"), seq_break = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE), expected_output = c(NA,
1, 2, NA, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, NA, NA, 4, 5, 6, 6, NA, NA, 7, 7, 7, NA, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, NA, NA, 11, 11, NA, 12)), .Names = c("group", "seq_break",
"expected_output"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-50L))
How can I achieve that with tidyverse
?
回答1:
A solution using tidyverse
and data.table
. Assuming dt1
is your example data frame and dt3
is the final output. Notice that I think in the expected output, row 47 to 48 should be 9, and row 50 should be 10. I am not sure why in your expected output row 47 to 48 is 11 and row 50 is 12.
library(tidyverse)
library(data.table)
dt2 <- dt1 %>% rowid_to_column()
dt3 <- dt2 %>%
mutate(ID = rleid(group, seq_break)) %>%
group_by(group, seq_break, ID) %>%
filter(!(is.na(group) & seq_break & row_number() > 1)) %>%
ungroup() %>%
mutate(ID2 = cumsum(seq_break)) %>%
drop_na(group) %>%
mutate(expected_output = rleid(group, ID2)) %>%
select(rowid, expected_output) %>%
left_join(dt2, ., by = "rowid") %>%
select(-rowid)
dt3
# # A tibble: 50 x 3
# group seq_break expected_output
# <chr> <lgl> <int>
# 1 NA TRUE NA
# 2 A FALSE 1
# 3 B FALSE 2
# 4 NA TRUE NA
# 5 B FALSE 3
# 6 B FALSE 3
# 7 B FALSE 3
# 8 B FALSE 3
# 9 B FALSE 3
# 10 B FALSE 3
# # ... with 40 more rows
来源:https://stackoverflow.com/questions/52344379/define-sequences-based-on-a-variable-run-with-additional-condition-from-another