I have the following data:
x <- c(F, T, T, T, F, T, T, T, T, T)
names(x) <- letters[1:10]
y <- c(T, F, T, T, T, F, T, T, T, T)
names(y) <- letters[1:10]
z <- c(T, T, F, T, T, T, T, T, F, F)
names(z) <- letters[1:10]
a <- c(T, T, T, T, T, F, T, F, T, T, T, T, T)
names(a) <- letters[1:13]
I want to create a function which can subset the first 5 consecutive T
values, but from the back. For example, if I pass x
object through that function, I should get the following output:
# f g h i j
# TRUE TRUE TRUE TRUE TRUE
Or if I pass y
through it, I should just get an NA
. Because there are no first 5 T
values from the back.
z
has first 5 consecutive T
values in the middle and, hence, those should be returned.
# d e f g h
# TRUE TRUE TRUE TRUE TRUE
In a
, there are two sets of 5 consecutive values, in the beginning and in the end. Since, the first group from the back would be the one at the end and hence those values should be returned.
# i j k l m
# TRUE TRUE TRUE TRUE TRUE
How can I make this function?
rev(x)[rev(x)][1:5]
– Rhizopod