Tuesday 15 February 2011

r - How to count the number of consecutive numbers in a data.frame? -



r - How to count the number of consecutive numbers in a data.frame? -

i'm using r programming btw.

so have info frame this.

row.names tx hx tn 1 51 33.9 43.48 24.9 2 52 33.0 41.03 22.5 3 64 32.8 37.74 23.3 4 65 32.7 44.53 22.3 5 66 34.9 42.43 23.5 6 77 33.6 38.74 21.8 7 78 34.6 45.46 27.1 8 84 32.9 41.67 24.1 9 85 35.1 43.15 25.0 10 117 32.0 37.11 20.0

i'm trying figure out how many times 2 or more consective numbers , 3 or more consective nummbers occur in column row.names.

so illustration 51, 52 64, 65, 66 77, 78 , lastly 84, 85. want count how many times happens 2 or more consective numbers , 3 or more consective numbers.

my result be

2+ = 4 3+ = 1

thanks help in advance!

tmp <- table(rle(cumsum(c(1l, diff(df$row.names)) != 1l))$lengths) cumsum(rev(tmp)) #3 2 1 #1 4 5

r matrix data.frame

No comments:

Post a Comment