Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
R 从变量中出现值开始分组_R_Dplyr_Purrr_Tibble - Fatal编程技术网

R 从变量中出现值开始分组

R 从变量中出现值开始分组,r,dplyr,purrr,tibble,R,Dplyr,Purrr,Tibble,我有一个变量,看起来像: library(tibble) data <- tibble(toBeGrouped = c(TRUE, sample(c(TRUE, FALSE), 9, replace = TRUE))) data # A tibble: 10 x 1 toBeGrouped <lgl> 1 TRUE 2 FALSE 3 FALSE 4 FALSE 5 FALSE

我有一个变量,看起来像:

library(tibble)

data <- tibble(toBeGrouped = c(TRUE, sample(c(TRUE, FALSE), 9, replace = TRUE)))

data

# A tibble: 10 x 1
   toBeGrouped
   <lgl>      
 1 TRUE       
 2 FALSE      
 3 FALSE      
 4 FALSE      
 5 FALSE      
 6 TRUE       
 7 FALSE      
 8 FALSE      
 9 FALSE      
10 TRUE
库(TIBLE)

数据当
TRUE
值为1/0时,我们可以在逻辑列上使用
cumsum
添加1

library(dplyr)
data <- data %>%
           mutate(groups = cumsum(toBeGrouped))
库(dplyr)
数据%
突变(组=总和(组合))
数据
数据
library(dplyr)
data <- data %>%
           mutate(groups = cumsum(toBeGrouped))
data <- structure(list(toBeGrouped = c(TRUE, FALSE, FALSE, FALSE, FALSE, 
TRUE, FALSE, FALSE, FALSE, TRUE)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))