我在R中工作,有一些连续的数据。具体来说,我有一个整数列表,这些整数在不同的序列中多次出现。我想要做的是创建一些代码来识别有多少不同的序列出现。
目前,我正在手工操作。我预定义了存在的模式,并应用了一个统计事件的函数。
我首先使用RMYSQL进行查询,该查询存储在变量product_process_history_joined中。然后,创建一个存储在变量数据中的感兴趣数据的列表。然后,我定义了我的函数应该工作的模式,最后我应用我的函数来统计出现的次数。
守则:
product_process_history_joined<-dbGetQuery(con,"SELECT *
FROM product, process_history
WHERE product.idproduct = process_history.product_idproduct")
data<-product_process_history_joined$process_types_idprocess_types
pat <- c(1,2,4,5,6)
x <- sapply(1:(length(data)-length(pat)), function(x) all(data[x: (x+length(pat)-1)] == pat))
route<-data[which(x)]
countR<-length(route)
pat1 <- c(1,2,4,5,7,9,7,7,2,5,6,10)
x <- sapply(1:(length(data)-length(pat1)), function(x) all(data[x: (x+length(pat1)-1)] == pat1))
route1<-data[which(x)]
countR1<-length(route1)生成并存储在数据变量中的数据集如下所示:
[1] 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5
[36] 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 4
[71] 5 6 1 4 5 6 1 4 5 6 1 4 5 6 1 2 4 5 6 10 1 2 4 5 7 9 7 7 2 5 6 10 1 2 4
[106] 5 6 10 1 2 4 5 6 10 1 2 4 8 1 2 3 5 7 8 1 2 3 5 6 1 2 3 5 6 1 2 4 5 6 10这只是列表的一个子集。我使用了大约12种不同的模式。对于给定数据集中的前2种模式,pat为21,pat1为1。
发布于 2016-01-25 13:55:40
没有理由重新振作。你可以用rollapply
original_data <- c(1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5,6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 2, 4, 5, 6, 10, 1, 2, 4, 5, 7, 9, 7, 7, 2, 5, 6, 10, 1, 2, 4, 5, 6, 10, 1, 2, 4, 5, 6, 10, 1, 2, 4, 8, 1, 2, 3, 5, 7, 8, 1, 2, 3, 5, 6, 1, 2, 3, 5, 6, 1, 2, 4, 5, 6, 10)
pattern2 <- c(1, 4, 5, 6)
library(zoo)
sum(
rollapply(
original_data,
width = length(pattern2),
FUN = function(x, pattern) all(x == pattern),
pattern = pattern2
)
)
#[1] 21如果有必要,可以提供更快的解决方案,但这提供了良好的可读性。
编辑
这将提取以1开头的所有不同序列:
x <- split(original_data, cumsum(original_data == 1))
unique(x)
res <- vapply(unique(x), function(x, y) sum(vapply(y, FUN = identical, y = x, FUN.VALUE = TRUE)), y = x, FUN.VALUE = 1L)
Res <- data.frame(n = res,
seq = vapply(unique(x), paste, collapse = ",", FUN.VALUE = "a"))
# n seq
#1 21 1,4,5,6
#2 4 1,2,4,5,6,10
#3 1 1,2,4,5,7,9,7,7,2,5,6,10
#4 1 1,2,4,8
#5 1 1,2,3,5,7,8
#6 2 1,2,3,5,6发布于 2016-01-25 13:47:47
这肯定不是完成这项工作的最佳方法,但您可以决定将数据作为字符串处理,然后使用正则表达式(通过gregexpr)。
original_data <- c(1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5,6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 4, 5, 6, 1, 2, 4, 5, 6, 10, 1, 2, 4, 5, 7, 9, 7, 7, 2, 5, 6, 10, 1, 2, 4, 5, 6, 10, 1, 2, 4, 5, 6, 10, 1, 2, 4, 8, 1, 2, 3, 5, 7, 8, 1, 2, 3, 5, 6, 1, 2, 3, 5, 6, 1, 2, 4, 5, 6, 10)
data_as_string <- paste(original_data, collapse="-")
pattern1 = "1-2-4-5-6" # Your "pat"
pattern2 = "1-4-5-6" # Occurs 21 times in your data
pattern3 = "1-2-4-5-7-9-7-7-2-5-6-10" # Your "pat1"
gregexpr(pattern1,data_as_string)
# [[1]]
# [1] 169 207 220 273
# attr(,"match.length")
# [1] 9 9 9 9
# attr(,"useBytes")
# [1] TRUE
# So if you just want the number of occurrences
length(gregexpr(pattern1,data_as_string)[[1]])
# [1] 4
length(gregexpr(pattern2,data_as_string)[[1]])
# [1] 21
length(gregexpr(pattern3,data_as_string)[[1]])
# [1] 1https://stackoverflow.com/questions/34992829
复制相似问题