我正在使用R编程btw。
所以说我有这样的数据框。
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
我想知道列row.names中出现两次或更多次连带数和三次或更多连续数的次数。
因此,对于这个例子51,52然后是64,65,66然后77,78,最后是84,85。我只想计算两次或更多次连续数和三次或更多连结数的发生次数。
我的结果将是
2+ = 4
3+ = 1
提前感谢您的帮助!
答案 0 :(得分:4)
tmp <- table(rle(cumsum(c(1L, diff(DF$row.names)) != 1L))$lengths)
cumsum(rev(tmp))
#3 2 1
#1 4 5
答案 1 :(得分:0)
我一直在努力,直到我看到@ akrun的评论。我从那里开始做了一些修改。这有效:
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