I'm trying to count "chunks" of similar consecutive numbers in a vector. But my code is spitting back "NA"s when I expect it to be putting in either a "1" or a "0"
x <- c(1,1,0,1,1,1,0,0,1)
y <- c(0)
turing <- function(x){
y <- c(0)
for (i in length(x)){
ifelse(isTRUE(x[i] == x[i+1]), y[i]<-0, y[i]<-1)
}
y
}
turing(x)
This spits out
[1] 0 NA NA NA NA NA NA NA 1
EDIT: The following seems to work fine:
turing <- function(x){
y <- numeric(length(x))
for (i in seq_along(x)){
ifelse(isTRUE(x[i] == x[i+1]), y[i] <-0 , y[i] <- 1)
}
y
}
As it spits out
0 1 1 0 0 1 0 1 1
Thanks!