Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
tidyverse:连续出现零_R_Dplyr_Tidyverse - Fatal编程技术网

tidyverse:连续出现零

tidyverse:连续出现零,r,dplyr,tidyverse,R,Dplyr,Tidyverse,要从a生成列b,以便 if a == 1 then b = 0 else b = counting start from 1 then if a == 1 then b = 0 else b = counting start from 1 如下文tb1所示 我的工作示例如下: library(tidyverse) tb1 <- tibble( a = c(1, rep(0, 4), 1, rep(0, 5), 1, 1, rep(0, 4), 1, rep(0, 2)

要从
a
生成列
b
,以便

if a == 1 then b = 0 
else b = counting start from 1
then if  a == 1 then b = 0 
else b = counting start from 1
如下文
tb1
所示

我的工作示例如下:

library(tidyverse)
tb1 <- 
  tibble(
    a = c(1, rep(0, 4), 1, rep(0, 5), 1, 1, rep(0, 4), 1, rep(0, 2))
  , b = c(0, 1:4, 0, 1:5, 0, 0, 1:4, 0, 1:2)
  )
tb1
#> # A tibble: 20 x 2
#>        a     b
#>    <dbl> <dbl>
#>  1     1     0
#>  2     0     1
#>  3     0     2
#>  4     0     3
#>  5     0     4
#>  6     1     0
#>  7     0     1
#>  8     0     2
#>  9     0     3
#> 10     0     4
#> 11     0     5
#> 12     1     0
#> 13     1     0
#> 14     0     1
#> 15     0     2
#> 16     0     3
#> 17     0     4
#> 18     1     0
#> 19     0     1
#> 20     0     2

tb2 <-
  tb1 %>% 
  mutate(b1 = if_else(condition = a == 1, true = 0, false = 1)) %>% 
  group_by(a) %>% 
  mutate(b2 = cumsum(b1))

tb2
#> # A tibble: 20 x 4
#> # Groups:   a [2]
#>        a     b    b1    b2
#>    <dbl> <dbl> <dbl> <dbl>
#>  1     1     0     0     0
#>  2     0     1     1     1
#>  3     0     2     1     2
#>  4     0     3     1     3
#>  5     0     4     1     4
#>  6     1     0     0     0
#>  7     0     1     1     5
#>  8     0     2     1     6
#>  9     0     3     1     7
#> 10     0     4     1     8
#> 11     0     5     1     9
#> 12     1     0     0     0
#> 13     1     0     0     0
#> 14     0     1     1    10
#> 15     0     2     1    11
#> 16     0     3     1    12
#> 17     0     4     1    13
#> 18     1     0     0     0
#> 19     0     1     1    14
#> 20     0     2     1    15
库(tidyverse)
tb1#A tible:20 x 2
#>a b
#>     
#>  1     1     0
#>  2     0     1
#>  3     0     2
#>  4     0     3
#>  5     0     4
#>  6     1     0
#>  7     0     1
#>  8     0     2
#>  9     0     3
#> 10     0     4
#> 11     0     5
#> 12     1     0
#> 13     1     0
#> 14     0     1
#> 15     0     2
#> 16     0     3
#> 17     0     4
#> 18     1     0
#> 19     0     1
#> 20     0     2
tb2%
变异(b1=if_else(条件=a==1,真=0,假=1))%>%
组别(a)%>%
突变(b2=cumsum(b1))
tb2
#>#A tibble:20 x 4
#>#组:a[2]
#>a b b1 b2
#>       
#>  1     1     0     0     0
#>  2     0     1     1     1
#>  3     0     2     1     2
#>  4     0     3     1     3
#>  5     0     4     1     4
#>  6     1     0     0     0
#>  7     0     1     1     5
#>  8     0     2     1     6
#>  9     0     3     1     7
#> 10     0     4     1     8
#> 11     0     5     1     9
#> 12     1     0     0     0
#> 13     1     0     0     0
#> 14     0     1     1    10
#> 15     0     2     1    11
#> 16     0     3     1    12
#> 17     0     4     1    13
#> 18     1     0     0     0
#> 19     0     1     1    14
#> 20     0     2     1    15
一个选项可以是:

tb1 %>%
 group_by(rleid = with(rle(a), rep(seq_along(lengths), lengths))) %>%
 mutate(b = 1:n() * (a != 1)) 

       a     b rleid
   <dbl> <int> <int>
 1     1     0     1
 2     0     1     2
 3     0     2     2
 4     0     3     2
 5     0     4     2
 6     1     0     3
 7     0     1     4
 8     0     2     4
 9     0     3     4
10     0     4     4
11     0     5     4
12     1     0     5
13     1     0     5
14     0     1     6
15     0     2     6
16     0     3     6
17     0     4     6
18     1     0     7
19     0     1     8
20     0     2     8
tb1%>%
分组依据(rleid=具有(rle(a),代表(沿(长度)的顺序),长度))%>%
变异(b=1:n()*(a!=1))
一个b字
1     1     0     1
2     0     1     2
3     0     2     2
4     0     3     2
5     0     4     2
6     1     0     3
7     0     1     4
8     0     2     4
9     0     3     4
10     0     4     4
11     0     5     4
12     1     0     5
13     1     0     5
14     0     1     6
15     0     2     6
16     0     3     6
17     0     4     6
18     1     0     7
19     0     1     8
20     0     2     8

首先,按以
a=1开头的每个区块分组,这可以通过
cumsum(a)
完成。然后,在每个这样的组中,对
a
的值进行计数,这些值等于
0
,如下所示:

tb1 %>% 
  group_by(grouper = cumsum(a)) %>% 
  mutate(b = cumsum(a == 0)) %>% 
  ungroup() %>% 
  select(-grouper)

       a     b
   <dbl> <int>
 1     1     0
 2     0     1
 3     0     2
 4     0     3
 5     0     4
 6     1     0
 7     0     1
 8     0     2
 9     0     3
10     0     4
11     0     5
12     1     0
13     1     0
14     0     1
15     0     2
16     0     3
17     0     4
18     1     0
19     0     1
20     0     2
tb1%>%
分组依据(grouper=cumsum(a))%>%
变异(b=cumsum(a==0))%>%
解组()%>%
选择(-gropper)
a b
1     1     0
2     0     1
3     0     2
4     0     3
5     0     4
6     1     0
7     0     1
8     0     2
9     0     3
10     0     4
11     0     5
12     1     0
13     1     0
14     0     1
15     0     2
16     0     3
17     0     4
18     1     0
19     0     1
20     0     2