Repeat or Combine Argument

I am familiar with R since I have been using it for several years. Unfortunately, I'm not very good at creating functions that include looping or repeating an equation. The problem is this:

I have a vector containing more than 1000 values. I would like to calculate the absolute difference between two comparable means of equal size from a subset of this vector.

Here is an example.

I have a vector (vec) of length 8

[1] 0.12472963 1.15341289 -1.09662288 -0.73241639 0.06437658 -0.13647136 -1.52592048 1.46450084 

I would like to calculate the average of the first two values ​​(0.12472963, 1.15341289) and get the absolute difference with the average of the following values ​​(-1.09662288 -0.73241639), after which, working along the way down the vector.

In this case, I can easily use the following equation:

 abs(mean(vec[1:2])-mean(vec[3:4])) 

and gradually increase each number by 1 to manually work until the end of the vector. I would get the following vector.

 [1] 1.553591 0.3624149 0.8784722 0.497176 0.005337574 

However, I would like to have an automatic procedure that allows me to do this over long vectors and change the number of values ​​from which the means can be calculated.

It seems to me that this should be relatively simple, but I do not know where to start.

+5
source share
3 answers

Use filter :

 c(abs(filter(vec, c(0.5, 0.5, -0.5, -0.5), sides=1)[-(1:3)])) #[1] 1.55359090 0.36241491 0.87847224 0.49717601 0.00533757 
+7
source

Using rollapply from zoo

  library(zoo) n <- 2 n1 <- length(vec) abs(rollapply(vec[1:(n1-n)], 2, mean)-rollapply(vec[(n+1):n1], 2,mean)) #[1] 1.55359090 0.36241491 0.87847224 0.49717601 0.00533757 

In addition, other variants of the above code (from the comments of @G. Grothendieck - one of the authors of the zoo package)

  abs(rollmean(vec[1:(n1-n)], 2) - rollmean(vec[(n+1):n1], 2)) #using #`rollmean` instead of `rollapply` 

or

  rollapply(vec, 4, function(x) abs(mean(x[1:2]) - mean(x[3:4]))) 

or

  abs(rollapply(vec, 4, "%*%", c(1, 1, -1, -1)/2)) 
+2
source

As always, I call using:

 vec<-rep(c( 0.12472963 , 1.15341289, -1.09662288, -0.73241639 , 0.06437658, -0.13647136 ,-1.52592048 , 1.46450084 ),100) microbenchmark(roland(vec),akrun(vec),times=3) Unit: microseconds expr min lq mean median uq max roland(vec) 564.128 565.2275 647.3353 566.327 688.939 811.551 akrun(vec) 3717.410 3982.1535 4218.3057 4246.897 4468.753 4690.610 neval 3 3 
+1
source

Source: https://habr.com/ru/post/1205296/


All Articles