R - counting adjacent duplicate items

前端 未结 1 699
猫巷女王i
猫巷女王i 2021-01-29 07:38

New to R and would like to do the following operation: I have a set of numbers e.g. (1,1,0,1,1,1,0,0,1) and need to count adjacent duplicates as they occur. The result I am look

1条回答
  •  梦毁少年i
    2021-01-29 08:16

    We can use rle

    rle(v1)$lengths
    #[1] 2 1 3 2 1
    

    data

    v1 <- c(1,1,0,1,1,1,0,0,1) 
    

    0 讨论(0)
提交回复
热议问题