基于列表中的公共值从data.frame中提取行

基于列表中的公共值从data.frame中提取行,r,list,filtering,R,List,Filtering,我正在寻找一种基于数字序列列表从data.frame中筛选行的简单方法 下面是一个例子: 我的初始数据帧: data <- data.frame(x=c(0,1,2,0,1,2,3,4,5,12,2,0,10,11,12,13),y="other_data") data为什么不从zoo使用rollapply: library(zoo) ind = lapply(list1, function(x) { n = length(x) which(rollapply(data

我正在寻找一种基于数字序列列表从data.frame中筛选行的简单方法

下面是一个例子:

我的初始数据帧:

data <- data.frame(x=c(0,1,2,0,1,2,3,4,5,12,2,0,10,11,12,13),y="other_data")

data为什么不从
zoo
使用
rollapply

library(zoo)

ind = lapply(list1, function(x) {
    n = length(x)
    which(rollapply(data$x, n, function(y) all(y==x))) + 0:(n-1)
})

data[unlist(ind),]
#x          y
#5   1 other_data
#6   2 other_data
#7   3 other_data
#8   4 other_data
#9   5 other_data
#13 10 other_data
#14 11 other_data
#15 12 other_data
#16 13 other_data

我从一个自定义函数开始为一个序列获取子集,然后很容易用lappy进行扩展

#function that takes sequence and a vector
#and returns indices of vector that have complete sequence
get_row_indices<- function(sequence,v){
  #get run lengths of whether vector is in sequence
  rle_d <- rle(v %in% sequence)
  #test if it's complete, so both v in sequence and length of 
  #matches is length of sequence
  select <- rep(length(sequence)==rle_d$lengths &rle_d$values,rle_d$lengths)

  return(select)

}


#add row ID to data to show selection
data$row_id <- 1:nrow(data)
res <- do.call(rbind,lapply(list1,function(x){
  return(data[get_row_indices(sequence=x,v=data$x),])
}))

res

> res
    x          y row_id
5   1 other_data      5
6   2 other_data      6
7   3 other_data      7
8   4 other_data      8
9   5 other_data      9
13 10 other_data     13
14 11 other_data     14
15 12 other_data     15
16 13 other_data     16
采用序列和向量的函数 #并返回具有完整序列的向量的索引
获取行索引函数
match2
遍历每个
x
值,并对照长度为n的向量检查它和下一个n值。然后使用
Reduce
创建索引序列

match2 <- function(vec) {
  start <- which(sapply(1:nrow(data), function(i) all(data$x[i:(i+length(vec)-1)] == vec)))
  Reduce(':', c(start,start+length(vec)-1))
}
结果:

    x          y
5   1 other_data
6   2 other_data
7   3 other_data
8   4 other_data
9   5 other_data
13 10 other_data
14 11 other_data
15 12 other_data
16 13 other_data

如果列
y
包含
c(“其他_数据”,“数据”,rep(“其他_数据”,14))
,那么期望的输出是什么?请使用
数据我知道这种感谢评论是不受欢迎的,但我在如何使用rollapply上挣扎了一段时间,所以感谢itNp,我从其他使用unknown(从我的角度)中学到了很多东西功能也一样!谢谢你帮助赫罗卡!您的自定义函数工作正常:)
match2 <- function(vec) {
  start <- which(sapply(1:nrow(data), function(i) all(data$x[i:(i+length(vec)-1)] == vec)))
  Reduce(':', c(start,start+length(vec)-1))
}
s <- sapply(list1, match2)
data[unlist(s),]
#     x          y
# 5   1 other_data
# 6   2 other_data
# 7   3 other_data
# 8   4 other_data
# 9   5 other_data
# 13 10 other_data
# 14 11 other_data
# 15 12 other_data
# 16 13 other_data
extract_fun <- function(x, dat){
  # Index where the sequences start
  ind <- which(dat == x[1])
  # Indexes (within dat) where the sequence should be
  ind_seq <- lapply(ind, seq, length.out = length(x))
  # Extract the values from dat at the position
  dat_val <- mapply(`[`, list(dat), ind_seq)
  # Check if values within dat == those in list1
  i <- which(as.logical(apply(dat_val, 2, all.equal, x))) # which one is equal?
  # Return the correct indices
  ind_seq[[i]]
}
all_ind <- do.call(c, lapply(list1, extract_fun, data$x))
data[all_ind,]
    x          y
5   1 other_data
6   2 other_data
7   3 other_data
8   4 other_data
9   5 other_data
13 10 other_data
14 11 other_data
15 12 other_data
16 13 other_data