Questions when using the "rle" function

2

I'm using the rle function.

x = rnorm(10,0,1)
l = 1
teste = rle(x > l)

evento = teste$lengths[teste$values=="TRUE"]

I'm having trouble extracting the values from my x vector, which has test $ values == "TRUE". That is, I want to know the values and not the positions they meet.

NOTE: I want to do this by using the rle function.

    
asked by anonymous 25.09.2015 / 01:32

2 answers

1

Your error is in using "TRUE" , which is a string, instead of TRUE , which is a logical value. The correct one, using your code, would be

evento = teste$lengths[teste$values==TRUE]

But in fact, any comparison of type ==TRUE is redundant, because if x == TRUE results in TRUE , the value x is already TRUE and no comparison is necessary. You can simply use the following:

evento = teste$lengths[teste$values]

Or, if you wanted to avoid repeating teste :

with(teste, lengths[values])
    
25.09.2015 / 03:25
0

What I wanted to do was this:

x = rnorm(10,0,1)
limite = 1
teste = rle(x > limite)

evento = which(teste$values == TRUE)
acumulada_lengths= cumsum(teste$lengths) 
fim = acumulada_lengths[evento]

auxiliar = ifelse(evento>1, evento-1, 0)
inicio = acumulada_lengths[auxiliar] + 1
if (0 %in% auxiliar) inicio = c(1,inicio) else inicio = inicio

obs = x[inicio[1]:fim[1]]
    
26.09.2015 / 15:07