LoginSignup
0
0

More than 5 years have passed since last update.

ベクトルの繰り返し要素を1つにして返す

Posted at

やり方

> v <- c(1, 1, 5, 5, 5, 5, 2, 2, 6, 6, 1, 3, 3)
> v[v != dplyr::lag(v, default = !v[1])]
[1] 1 5 2 6 1 3

なぜできるか

lagが一つずらしたものを返すので、それを使う

> dplyr::lag(v, default = !v[1])
 [1] 0 1 1 5 5 5 5 2 2 6 6 1 3
> v
 [1] 1 1 5 5 5 5 2 2 6 6 1 3 3

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0