将向量拆分为块直到事件发生

时间:2016-05-09 22:18:42

标签: r vector dataframe

我有一个硬币翻转的向量x <- c("H","H","T","H","T","T","H","H","H","H","T")。我想从上面的向量构造一个包含两个变量(模式,长度)的数据框。

模式字段将类似于{H H T,H T,T,H H H H T},相应的长度将为{2,1,0,4}

我的意图是计算连续的头部,直到硬币翻转出现尾部。

2 个答案:

答案 0 :(得分:4)

给出向量

x <- c("H","H","T","H","T","T","H","H","H","H","T")

diff(c(0, as.integer(gregexpr("T", paste0(x, collapse = ""))[[1]]) ) ) - 1
# [1] 2 1 0 4

<强>解释

将矢量折叠成字符串

paste0(x, collapse = "")
## "HHTHTTHHHHT"

找到所有T s

的位置
as.integer(gregexpr("T", paste0(x, collapse = ""))[[1]])
## [1]  3  5  6 11

H之间T的数量将是每个值之间的差值,减去1

附加前导0并找出每个值之间的差异。

diff(c(0, as.integer(gregexpr("T", paste0(x, collapse = ""))[[1]]) ) ) - 1
[1] 2 1 0 4

答案 1 :(得分:1)

您可以使用rle计算T之间的所有H,然后添加零H系列:

x  <- c("H","H","T","H","T","T","H","H","H","H","T")
y <- rle(x=='T')
table(c(rep(0,sum(y$lengths[y$values & y$lengths>1]-1)),y$lengths[!y$values]))


0 1 2 4 
1 1 1 1