简体   繁体   English

rollapply与函数rle(x)

[英]rollapply with function rle(x)

I have time series data as data.table class and each column (observation points) has values that I want to count them within sliding window (30 width). 我有时间序列数据作为data.table类,每列(观察点)都有我想在滑动窗口(30宽度)内计算它们的值。 I tried to use rle(sort(x)) to count each values within rollapply but it's not working. 我试图使用rle(sort(x))来计算rollapply中的每个值,但它不起作用。

for example if I have table like below, 例如,如果我有如下表,

dt <- data.frame(v1=c(1,0,1,4,4,4,4,4),v2=c(1,1,1,4,3,3,3,3),
          v3=c(0,1,1,3,3,3,3,2),v4=c(1,1,0,3,3,3,3,3),
       v5=c(1,1,1,5,5,5,5,5))

I tried like this; 我试过这个;

rollapply(dt, 3, function(x) {rle(sort(x))$values; rle(sort(x))$length})

but the result is just doesn't make sense. 但结果却没有意义。 please give me some direction... 请给我一些方向......

Solution 1 Assuming the objective is to get rolling counts of 3 values try the following: 解决方案1假设目标是获得3个值的滚动计数,请尝试以下操作:

m <- as.matrix(dt)
levs <- sort(unique(c(m)))
f <- function(x) table(factor(x, levs))
r <- rollapply(m, 3, f)

Here levs is 0, 1, ..., 5 so for each application of the function we will get out a vector 6 long witih a count of the 0's, 1's, ..., 5's. 这里levs是0,1,...,5因此对于函数的每个应用程序,我们将得到一个向量6长度为0,1,...,5的计数。 There are 5 input columns so applying such a function to each column gives 5 * 6 = 30 columns of output. 有5个输入列,因此对每列应用这样的函数给出5 * 6 = 30列输出。

Note that rollapply works with matrices or zoo objects, not data frames, so we converted it. 请注意, rollapply适用于矩阵或动物园对象,而不是数据框,因此我们对其进行了转换。 Also to ensure that each function application outputs a vector of the same length we convert each input to a factor with the same levels. 另外,为了确保每个函数应用程序输出相同长度的向量,我们将每个输入转换为具有相同级别的因子。

Note that: 注意:

ra <- array(r, c(6, 6, 5))

gives a 3d array in which ra[,,i] is the matrix formed by rollapply(dt[, i], 3, f) . 给出一个3d数组,其中ra [i]是由rollapply(dt[, i], 3, f)形成的矩阵。 That is, in the matrix ra[,,i] there is a row for each application of f on column i and the columns in that row count the number of 0's, 1's, ..., 5's. 也就是说,在矩阵ra[,,i] ,对于列i上的f每个应用,存在一行,并且该行中的列计数0,1,...,5的数量。

Another possibility is this which gives the same 5 matrices (one per input column) as components of the resulting list: 另一种可能性是,它将相同的5个矩阵(每个输入列一个)作为结果列表的组件:

lapply(dt, rollapply, 3, f)

For example, consider the following. 例如,请考虑以下内容。 Row 1 of the output says that the first application of f on dt[,1] has one 0, two 1s and no other values. 输出的第1行表示ft在dt[,1]上的第一次应用有一个0,两个1,没有其他值。 This can also be obtained from r[,,1] or from lapply(dt, rollapply, 3, f)[[1]] : 这也可以从r[,,1]lapply(dt, rollapply, 3, f)[[1]]

> rollapply(dt[, 1], 3, f)
     0 1 2 3 4 5
[1,] 1 2 0 0 0 0  <- dt[1:3,1] has 1 zero and 2 ones
[2,] 1 1 0 0 1 0  <- dt[2:4,1] has 1 zero and 1 one and 1 four, etc.
[3,] 0 1 0 0 2 0
[4,] 0 0 0 0 3 0
[5,] 0 0 0 0 3 0
[6,] 0 0 0 0 3 0

Solution 2 解决方案2

This says looking at cell 1,1 of the output that the there is one 0 and two 1s in dt[1:3,1] . 这说看输出的单元格1,1在dt[1:3,1]有一个0和两个1。 Looking at cell 2,1 of the output we see that there is one 0, one 1 and 1 four in dt[2:4,1] , etc. 查看输出的单元格2,1我们看到dt[2:4,1]中有一个0,1个1和1个等等。

> g <- function(x) { tab <- table(x); toString(paste(names(tab), tab, sep = ":")) }
> sapply(dt, rollapply, 3, g) # or rollapply(m, 3, g) where m was defined in solution 1
     v1              v2              v3         v4              v5        
[1,] "0:1, 1:2"      "1:3"           "0:1, 1:2" "0:1, 1:2"      "1:3"     
[2,] "0:1, 1:1, 4:1" "1:2, 4:1"      "1:2, 3:1" "0:1, 1:1, 3:1" "1:2, 5:1"
[3,] "1:1, 4:2"      "1:1, 3:1, 4:1" "1:1, 3:2" "0:1, 3:2"      "1:1, 5:2"
[4,] "4:3"           "3:2, 4:1"      "3:3"      "3:3"           "5:3"     
[5,] "4:3"           "3:3"           "3:3"      "3:3"           "5:3"     
[6,] "4:3"           "3:3"           "2:1, 3:2" "3:3"           "5:3"     

ADDED: Additional discussion and solution 2. 补充:补充讨论和解决方案2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM