Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在两个模式引用之间提取数据_R_Pattern Matching_Between - Fatal编程技术网

R 在两个模式引用之间提取数据

R 在两个模式引用之间提取数据,r,pattern-matching,between,R,Pattern Matching,Between,我试图在两种模式之间提取数据。也就是说,如果模式发生,则在该模式再次出现之前,所有数据都将被子集。然后我需要给这个子集一个数字,这样它就可以被识别 使用(R) 示例数据: DF<-(structure(list(date.time = structure(c(1374910680, 1374911040, 1374911160, 1374911580, 1374913380, 1374913500, 137491362

我试图在两种模式之间提取数据。也就是说,如果模式发生,则在该模式再次出现之前,所有数据都将被子集。然后我需要给这个子集一个数字,这样它就可以被识别

使用(R)

示例数据:

DF<-(structure(list(date.time = structure(c(1374910680, 1374911040, 
                                   1374911160, 1374911580, 1374913380, 1374913500, 1374913620, 1374913740, 
                                   1374914160, 1374914400, 1374914520, 1374914940, 1374915000, 1374915120, 
                                   1374915240), class = c("POSIXct", "POSIXt"), tzone = ""), aerial = structure(c(2L, 
                                                                                                                  2L, 8L, 8L, 2L, 2L, 2L, 8L, 8L, 8L, 2L, 2L, 8L, 2L, 2L), .Label = c("0", 
                                                                                                                                                                                      "1", "10", "11", "2", "3", "4", "5", "6", "7", "8", "9", "m"), class = "factor")), .Names = c("date.time", 
                                                                                                                                                                                                                                                                                    "aerial"), row.names = c(1L, 2L, 3L, 4L, 5L, 
                                                                                                                                                                                                                                                                                                            6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
                                                                                                                                                                                                                                                                                                             14L, 15L), class = "data.frame") )
我可以确定模式:

require(zoo)
library(zoo)

pat <- c(1,1)

x <- rollapply(DF$aerial, length(pat), FUN=function(x) all(x == pat))

DF[which(x),]
require(动物园)
图书馆(动物园)

pat似乎排除所有长度至少为2的1的运行就足够了,因此请尝试以下方法:

library(zoo)

a <- as.numeric(as.character(DF$aerial))
r <- rle(a)
cond <- with(r, values != 1 | lengths < 2)
ok <- rep(cond, r$lengths)
occur <- rep(cumsum(cond), r$lengths)
cbind(DF, occur)[ok, ]
修订:添加了
发生

is.between <- function(x, a, b) {
x > a & x < b
}
library(zoo)

a <- as.numeric(as.character(DF$aerial))
r <- rle(a)
cond <- with(r, values != 1 | lengths < 2)
ok <- rep(cond, r$lengths)
occur <- rep(cumsum(cond), r$lengths)
cbind(DF, occur)[ok, ]
             date.time aerial occur
3  2013-07-27 03:46:00      5     1
4  2013-07-27 03:53:00      5     1
8  2013-07-27 04:29:00      5     2
9  2013-07-27 04:36:00      5     2
10 2013-07-27 04:40:00      5     2
13 2013-07-27 04:50:00      5     3