I have a vector x made up of "Y" and "N".
> x
[1] Y N N N N N N N N N N Y N N N N
I would like to split this into
> x1
[1] Y N N N N N N N N N N
and
> x2
[1] Y N N N N
These vectors always start with a "Y" and take in all "N"s before the next "Y". Suggestions on how to do this?
You can use a combination of cumsum
and split
to do this with a one-liner:
x <- c("Y","N","N","N","N","N","N","N","N","N","N","Y","N","N","N","N")
v <- split(x,paste("x",cumsum(x=="Y"),sep=""))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With