使用R的斑点图案

q7solyqu  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(110)

我有一些非常简单的数据;

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)
qxsslcnc

qxsslcnc1#

正如PaulStaffordAllen所建议的,行程长度编码(rle)在这里起作用:

r <- rle(df_example$outcome)
r
# Run Length Encoding
#   lengths: int [1:8] 1 1 1 1 2 3 1 2
#   values : chr [1:8] "FALSE" "TRUE" "FALSE" "TRUE" "FALSE" "TRUE" "FALSE" "TRUE"

有了这一点,我们可以找到所有小于3的长度,并强制它们为false。

r$values[r$lengths < 3] <- "FALSE"
df_example$pattern2 <- inverse.rle(r)
df_example
#     id outcome pattern pattern2
# 1  jan   FALSE   FALSE    FALSE
# 2  feb    TRUE   FALSE    FALSE
# 3  mar   FALSE   FALSE    FALSE
# 4  apr    TRUE   FALSE    FALSE
# 5  may   FALSE   FALSE    FALSE
# 6  jun   FALSE   FALSE    FALSE
# 7  jul    TRUE    TRUE     TRUE
# 8  aug    TRUE    TRUE     TRUE
# 9  sep    TRUE    TRUE     TRUE
# 10 oct   FALSE   FALSE    FALSE
# 11 nov    TRUE   FALSE    FALSE
# 12 dec    TRUE   FALSE    FALSE

(Note:你处理的是字符串,而不是布尔值/逻辑值。如果你打算把它当作布尔值,你可以用df_example$outcome <- (df_example$outcome == "TRUE")来修复它。rle代码仍然可以工作,尽管把<- "FALSE"改为<- FALSE。)

相关问题