我的df看起来像这样:
我希望获得至少连续2次出现1的所有姓名。
在这种情况下,我只想回到B C D F。
有没有办法做到这一点?
答案 0 :(得分:5)
我们可以循环遍历行,使用rle
(查找是否存在连续元素)并创建逻辑索引以对' Names'
df1$Name[apply(df1[-1], 1, function(x) {
rl <- rle(x==1)
any(rl$lengths[rl$values]>=2)})]
#[1] "B" "C" "D" "F"
更快的方法可能是paste
每行中的元素,然后使用正则表达式查找以查找1是否后跟1
df1$Name[grepl("(?<=1)1", do.call(paste0, df1[-1]), perl = TRUE)]
#[1] "B" "C" "D" "F"
df1 <- structure(list(Name = c("A", "B", "C", "D", "E", "F"), `2000` = c(1L,
0L, 1L, 1L, 0L, 0L), `2001` = c(0L, 0L, 1L, 1L, 1L, 1L), `2002` = c(0L,
1L, 0L, 1L, 0L, 0L), `2003` = c(1L, 1L, 0L, 1L, 1L, 1L), `2004` = c(0L,
0L, 1L, 1L, 0L, 1L), `2005` = c(1L, 1L, 1L, 0L, 1L, 1L)), .Names = c("Name",
"2000", "2001", "2002", "2003", "2004", "2005"), class = "data.frame",
row.names = c(NA, -6L))
答案 1 :(得分:3)
使用lag
,这只适用于连续2,如果您需要增加限额,可以查看akrun's answer
s=df$Name[apply(df[,c(2:6)],1,function(x) any((lag(x)==x)&x==1))]
s[!is.na(s)]
[1] "B" "C" "D" "F"
答案 2 :(得分:2)
使用zoo::rollapply
的选项:
library(zoo)
#One can increase the n for 3, 4 etc consecutive values
# For consecutive 2 values
n =2
df1[apply(df1[-1], 1, function(x)
length(which(rollapply(x, width=n, function(x)all(x==rep(1,n)), align = "left")))>0),1]
#[1] "B" "C" "D" "F"