coffeewithR
coffeewithR

Reputation: 13

Why is rle in R returning incorrect lengths?

I am using the function 'rle' to calculate how many consecutive values there are in my vector. The first part of my vector is:

16992 32 96 8
48
144
8
8
8
8
8
8
8
8
8
8
8

but rle returns lengths of '1 1 1 1 1 1 2 1 1 1 4 1 1'

and values '16992 32 96 8 48 144 8 8 8 8 8 8 8 '

There are clearly 11 consecutive 8s. Please can someone help? Thank you in advance.

Update: I used dput() to view my data and it showed:

c(16992, 31.9999997615814, 96, 8, 48, 144, 8, 8, 8.00000023841858, 8, 7.99999976158142, 8, 8, 8, 8, 8.00000023841858, 7.99999976158142).

After seeing this, it looks like the values are sometimes just below or above 8, although this doesn't show when I View() the vector. Is this is the issue?

I am a bit confused as to why the values are not rounded to the nearest whole number. I applied the diff() function to these time points beforehand...

c(1390456832,
1390473824,
    1390473856,
    1390473952,
    1390473960,
    1390474008,
    1390474152,
1390474160,
1390474168,
1390474176,
    1390474184,
    1390474192,
    1390474200,
    1390474208,
    1390474216,
    1390474224,
    1390474232)

Upvotes: 1

Views: 246

Answers (1)

akrun
akrun

Reputation: 887571

The values were floats and they are not equal. Check Why are these numbers not equal? . One option is to round and apply the rle

rle(round(v1))$lengths
#[1]  1  1  1  1  1  1 11

data

v1 <- c(16992, 31.9999997615814, 96, 8, 48, 144, 8, 8, 
   8.00000023841858, 8, 7.99999976158142, 8, 8, 8, 8, 
    8.00000023841858, 7.99999976158142)

Upvotes: 0

Related Questions