如果为 FALSE,则增加列,如果为 TRUE,则停止增加(tidyverse,dplyr,R)
increment column if FALSE, stop incrementing if TRUE (tidyverse, dplyr, R)
我有一个 table 看起来像这样:
data <- structure(list(group = c(0L, 0L, 1L, 2L), id = c("1", "2", "3",
"4"), m = c("ac1", "ac1", "ac1", "me0"), together = c(FALSE,
FALSE, TRUE, TRUE)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-4L))
我想创建一个列 group
,在 together == FALSE
时递增,在 together == TRUE
时保持当前数字。
这是我想要的输出:
# A tibble: 4 x 4
id m together group
<chr> <chr> <lgl> <dbl>
1 1 ac1 FALSE 0
2 2 ac1 FALSE 1
3 3 ac1 TRUE 2
4 4 me0 TRUE 2
我已经尝试过像 这样的解决方案,但它并没有给我我想要的东西..
# A tibble: 4 x 4
# Groups: group [3]
id m together group
<chr> <chr> <lgl> <int>
1 1 ac1 FALSE 0
2 2 ac1 FALSE 0
3 3 ac1 TRUE 1
4 4 me0 TRUE 2
请看,在这种情况下,我希望组读取 0,1,2,2。有任何想法吗?非常感谢。
这是一个简洁的解决方案:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
data <- structure(list(id = c("1", "2", "3", "4"),
m = c("ac1", "ac1", "ac1", "me0"),
together = c(FALSE, FALSE, TRUE, TRUE)),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L))
data <- data %>% mutate(
group = cumsum(c(0, na.omit(lag(!data$together)))))
data
#> # A tibble: 4 × 4
#> id m together group
#> <chr> <chr> <lgl> <dbl>
#> 1 1 ac1 FALSE 0
#> 2 2 ac1 FALSE 1
#> 3 3 ac1 TRUE 2
#> 4 4 me0 TRUE 2
由 reprex package (v2.0.1)
于 2022-04-24 创建
解决这个问题的另一种方法如下
data %>%
mutate(grp2 = c(0, cumsum(!na.omit(together * lag(together)))))
# A tibble: 4 x 5
group id m together grp2
<int> <chr> <chr> <lgl> <dbl>
1 0 1 ac1 FALSE 0
2 0 2 ac1 FALSE 1
3 1 3 ac1 TRUE 2
4 2 4 me0 TRUE 2
使用评论中给出的数据和运行相同的代码:
data1 <- data.frame(together=c(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE))
data1 %>%
mutate(grp2 = c(0, cumsum(!na.omit(together * lag(together)))))
together grp2
1 FALSE 0
2 FALSE 1
3 TRUE 2
4 TRUE 2
5 FALSE 3
6 TRUE 4
我有一个 table 看起来像这样:
data <- structure(list(group = c(0L, 0L, 1L, 2L), id = c("1", "2", "3",
"4"), m = c("ac1", "ac1", "ac1", "me0"), together = c(FALSE,
FALSE, TRUE, TRUE)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-4L))
我想创建一个列 group
,在 together == FALSE
时递增,在 together == TRUE
时保持当前数字。
这是我想要的输出:
# A tibble: 4 x 4
id m together group
<chr> <chr> <lgl> <dbl>
1 1 ac1 FALSE 0
2 2 ac1 FALSE 1
3 3 ac1 TRUE 2
4 4 me0 TRUE 2
我已经尝试过像
# A tibble: 4 x 4
# Groups: group [3]
id m together group
<chr> <chr> <lgl> <int>
1 1 ac1 FALSE 0
2 2 ac1 FALSE 0
3 3 ac1 TRUE 1
4 4 me0 TRUE 2
请看,在这种情况下,我希望组读取 0,1,2,2。有任何想法吗?非常感谢。
这是一个简洁的解决方案:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
data <- structure(list(id = c("1", "2", "3", "4"),
m = c("ac1", "ac1", "ac1", "me0"),
together = c(FALSE, FALSE, TRUE, TRUE)),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L))
data <- data %>% mutate(
group = cumsum(c(0, na.omit(lag(!data$together)))))
data
#> # A tibble: 4 × 4
#> id m together group
#> <chr> <chr> <lgl> <dbl>
#> 1 1 ac1 FALSE 0
#> 2 2 ac1 FALSE 1
#> 3 3 ac1 TRUE 2
#> 4 4 me0 TRUE 2
由 reprex package (v2.0.1)
于 2022-04-24 创建解决这个问题的另一种方法如下
data %>%
mutate(grp2 = c(0, cumsum(!na.omit(together * lag(together)))))
# A tibble: 4 x 5
group id m together grp2
<int> <chr> <chr> <lgl> <dbl>
1 0 1 ac1 FALSE 0
2 0 2 ac1 FALSE 1
3 1 3 ac1 TRUE 2
4 2 4 me0 TRUE 2
使用评论中给出的数据和运行相同的代码:
data1 <- data.frame(together=c(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE))
data1 %>%
mutate(grp2 = c(0, cumsum(!na.omit(together * lag(together)))))
together grp2
1 FALSE 0
2 FALSE 1
3 TRUE 2
4 TRUE 2
5 FALSE 3
6 TRUE 4