我有一些非常简单的数据;
id <- c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
outcome <- c("FALSE","TRUE","FALSE","TRUE","FALSE","FALSE","TRUE","TRUE","TRUE","FALSE","TRUE","TRUE")
df_example <- data.frame(id = id, outcome= outcome)
我面临的挑战是我试图在一行中发现(n)个TRUE值的模式。如果我这样做了,那么我想添加第三个变量“pattern”,并将TRUE添加到该模式中的每一行。
希望有人能支持。
我一直在尝试group_by()和fill(),但没有太大的成功
我想要达到的是...
id <- c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
outcome <- c("FALSE","TRUE","FALSE","TRUE","FALSE","FALSE","TRUE","TRUE","TRUE","FALSE","TRUE","TRUE")
pattern <- c("FALSE","FALSE","FALSE","FALSE","FALSE","FALSE","TRUE","TRUE","TRUE","FALSE","FALSE","FALSE")
df_example <- data.frame(id = id, outcome= outcome, pattern = pattern)
1条答案
按热度按时间qxsslcnc1#
正如PaulStaffordAllen所建议的,行程长度编码(
rle
)在这里起作用:有了这一点,我们可以找到所有小于3的长度,并强制它们为false。
(Note:你处理的是字符串,而不是布尔值/逻辑值。如果你打算把它当作布尔值,你可以用
df_example$outcome <- (df_example$outcome == "TRUE")
来修复它。rle
代码仍然可以工作,尽管把<- "FALSE"
改为<- FALSE
。)