Doubts when using the "rle" function

Asked

Viewed 43 times

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 what are the values of my vector x, which has test$values=="TRUE". I mean, I want to know the values and not the position they are in.

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

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 reality, any comparison of the kind ==TRUE is redundant because if x == TRUE result in TRUE, the x value is already TRUE and there is no need to compare. You can simply use the following:

evento = teste$lengths[teste$values]

Or, in case I wanted to avoid the repetition of teste:

with(teste, lengths[values])
  • That’s more or less what I wanted to know! But I was able to do what I wanted to do. Still, thank you so much for your help.

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]]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.