Reputation: 75
I'm using R programming btw.
So say I have a data frame like 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 to figure out how many times two or more consective numbers and three or more consective nummbers occur in the column row.names.
So for this example 51, 52 then 64, 65, 66 then 77, 78 and lastly 84, 85. I just want a count for how many times this happens for two or more consective numbers and three or more consective numbers.
My result would be
2+ = 4
3+ = 1
Thanks for your help in advance!
Upvotes: 2
Views: 300
Reputation: 4784
I was struggling with this, until I saw @akrun's comment. I started there and made some modifications. This works:
df <- structure(list(
row.names = c(51L, 52L, 64L, 65L, 66L, 77L, 78L, 84L, 85L, 117L),
Tx = c(33.9, 33, 32.8, 32.7, 34.9, 33.6, 34.6, 32.9, 35.1, 32),
Hx = c(43.48, 41.03, 37.74, 44.53, 42.43, 38.74, 45.46, 41.67, 43.15, 37.11),
Tn = c(24.9, 22.5, 23.3, 22.3, 23.5, 21.8, 27.1, 24.1, 25, 20)),
.Names = c("row.names", "Tx", "Hx", "Tn"), class = "data.frame",
row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))
delta <- c(0, diff(df$row.names))
tally <- cumsum(delta!=1)
count <- table(table(tally[delta==1])+1)
twoplus <- sum(count[names(count) > 1.999])
threeplus <- sum(count[names(count) > 2.999])
twoplus
threeplus
Upvotes: 0
Reputation: 132626
tmp <- table(rle(cumsum(c(1L, diff(DF$row.names)) != 1L))$lengths)
cumsum(rev(tmp))
#3 2 1
#1 4 5
Upvotes: 4