我的数据框如下:
DENY SELECT ON OBJECT::BADGEACCESS TO [VMVN]
我想找到包含至少两个连续TRUE值的行。它将在新列“User”中返回值“1”。其他行将在“User”列中返回值“0”:
structure(list(ID = c(1, 2, 3, 4, 5, 6), A = c(FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE), B = c(FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE), C = c(FALSE, TRUE, FALSE, FALSE, FALSE, TRUE),
D = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE), E = c(TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE)), .Names = c("ID", "A", "B",
"C", "D", "E"), class = "data.frame", row.names = c(NA, 6L))
> Df
ID A B C D E
1 1 FALSE FALSE FALSE FALSE TRUE
2 2 FALSE FALSE TRUE FALSE TRUE
3 3 FALSE FALSE FALSE FALSE TRUE
4 4 FALSE FALSE FALSE FALSE TRUE
5 5 FALSE FALSE FALSE TRUE TRUE
6 6 FALSE FALSE TRUE TRUE TRUE
谢谢
答案 0 :(得分:3)
一种可能性是使用rle
:
Df$User <- apply(Df[, -1], 1, function(x)
as.numeric(any(rle(x)$lengths >= 2 & rle(x)$values)))
Df;
#ID A B C D E User
#1 1 FALSE FALSE FALSE FALSE TRUE 0
#2 2 FALSE FALSE TRUE FALSE TRUE 0
#3 3 FALSE FALSE FALSE FALSE TRUE 0
#4 4 FALSE FALSE FALSE FALSE TRUE 0
#5 5 FALSE FALSE FALSE TRUE TRUE 1
#6 6 FALSE FALSE TRUE TRUE TRUE 1
说明:rle
计算Df
每行中连续相等值的长度和值。然后,我们标记那些>=2
等于TRUE
的值的行。
Df <- structure(list(ID = c(1, 2, 3, 4, 5, 6), A = c(FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE), B = c(FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE), C = c(FALSE, TRUE, FALSE, FALSE, FALSE, TRUE),
D = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE), E = c(TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE)), .Names = c("ID", "A", "B",
"C", "D", "E"), class = "data.frame", row.names = c(NA, 6L))
答案 1 :(得分:0)
以下是使用rleid
data.table
的选项
library(data.table)
setDT(df)[melt(df, id.var = "ID")[, .(V1 = rleid(value)* value, value = value),
.(ID)][(value), .(User = as.integer(.N > 1)),.(ID, V1)], User := User, on = .(ID)]
df
# ID A B C D E User
#1: 1 FALSE FALSE FALSE FALSE TRUE 0
#2: 2 FALSE FALSE TRUE FALSE TRUE 0
#3: 3 FALSE FALSE FALSE FALSE TRUE 0
#4: 4 FALSE FALSE FALSE FALSE TRUE 0
#5: 5 FALSE FALSE FALSE TRUE TRUE 1
#6: 6 FALSE FALSE TRUE TRUE TRUE 1