R is the difference between the sums of consecutive pairs

I used the diff (x,) function on the dataset to find the difference between the strength for 0.0003s time intervals. The problem stems from such high sampling rates and measurement inaccuracies of the system that it does not provide any meaningful data.

I am wondering if it is possible to find the difference between 100 consecutive numbers. For example, "difference = (Fz101 + Fz102 ... Fz200) - (Fz1 + Fz2 ... Fz100)

I am currently dividing by 0.000333 (sample rate) as I am trying to find the peak value for (delta Fz) / (delta t.)

(max(diff(lift_S8_Intns40_chainno$Fz)))/0.00033333
[1] 40472.2

      

Take a look at the familiar question whether it helps me to help me. [link to similar question] [1] How to find the difference between the sums of consecutive pairs and triplets in R?

Any help is appreciated, but my skills are not very good at r, so please keep it as simple as possible.

Data structure

structure(list(Time = c(18.31266667, 18.313, 18.31333333, 18.31366667, 
18.314, 18.31433333, 18.31466667, 18.315, 18.31533333, 18.31566667, 
18.316, 18.31633333, 18.31666667, 18.317, 18.31733333, 18.31766667, 
18.318, 18.31833333, 18.31866667, 18.319, 18.31933333, 18.31966667, 
18.32, 18.32033333, 18.32066667, 18.321, 18.32133333, 18.32166667, 
18.322, 18.32233333, 18.32266667, 18.323, 18.32333333, 18.32366667, 
18.324, 18.32433333, 18.32466667, 18.325, 18.32533333, 18.32566667, 
18.326, 18.32633333, 18.32666667, 18.327, 18.32733333, 18.32766667, 
18.328, 18.32833333, 18.32866667, 18.329, 18.32933333, 18.32966667, 
18.33, 18.33033333, 18.33066667, 18.331, 18.33133333, 18.33166667, 
18.332, 18.33233333, 18.33266667, 18.333, 18.33333333, 18.33366667, 
18.334, 18.33433333, 18.33466667, 18.335, 18.33533333, 18.33566667, 
18.336, 18.33633333, 18.33666667, 18.337, 18.33733333, 18.33766667, 
18.338, 18.33833333, 18.33866667, 18.339, 18.33933333, 18.33966667, 
18.34, 18.34033333, 18.34066667, 18.341, 18.34133333, 18.34166667, 
18.342, 18.34233333, 18.34266667, 18.343, 18.34333333, 18.34366667, 
18.344, 18.34433333, 18.34466667, 18.345, 18.34533333, 18.34566667, 
18.346, 18.34633333, 18.34666667, 18.347, 18.34733333, 18.34766667, 
18.348, 18.34833333, 18.34866667, 18.349, 18.34933333, 18.34966667, 
18.35, 18.35033333, 18.35066667, 18.351, 18.35133333, 18.35166667, 
18.352, 18.35233333, 18.35266667, 18.353, 18.35333333, 18.35366667, 
18.354, 18.35433333, 18.35466667, 18.355, 18.35533333, 18.35566667, 
18.356, 18.35633333, 18.35666667, 18.357, 18.35733333, 18.35766667, 
18.358, 18.35833333, 18.35866667, 18.359, 18.35933333, 18.35966667, 
18.36, 18.36033333, 18.36066667, 18.361, 18.36133333, 18.36166667, 
18.362, 18.36233333, 18.36266667, 18.363, 18.36333333, 18.36366667, 
18.364, 18.36433333, 18.36466667, 18.365, 18.36533333, 18.36566667, 
18.366, 18.36633333, 18.36666667, 18.367, 18.36733333, 18.36766667, 
18.368, 18.36833333, 18.36866667, 18.369, 18.36933333, 18.36966667, 
18.37, 18.37033333, 18.37066667, 18.371, 18.37133333, 18.37166667, 
18.372, 18.37233333, 18.37266667, 18.373, 18.37333333, 18.37366667, 
18.374, 18.37433333, 18.37466667, 18.375, 18.37533333, 18.37566667, 
18.376, 18.37633333, 18.37666667, 18.377, 18.37733333, 18.37766667, 
18.378, 18.37833333, 18.37866667, 18.379), Fz = c(2751.996392, 
2751.614101, 2752.364284, 2751.993635, 2751.615723, 2751.995127, 
2751.989063, 2751.241442, 2749.366648, 2750.486962, 2748.242022, 
2747.498762, 2748.245589, 2747.4998, 2748.254604, 2748.631317, 
2749.006798, 2750.129349, 2749.38176, 2749.385262, 2748.640657, 
2750.13658, 2748.640073, 2749.393888, 2750.145076, 2751.274421, 
2750.157172, 2750.15688, 2750.165279, 2752.407479, 2752.042472, 
2753.550297, 2752.424309, 2750.927558, 2751.688556, 2751.317583, 
2752.819117, 2752.45398, 2752.836757, 2752.472626, 2754.353062, 
2754.725527, 2755.111158, 2754.376021, 2752.882448, 2752.510924, 
2754.006329, 2755.508722, 2756.638796, 2756.636202, 2757.76947, 
2756.284199, 2757.408096, 2758.165381, 2757.418894, 2758.172774, 
2758.178838, 2758.936415, 2758.56421, 2757.448825, 2757.824079, 
2758.569203, 2759.69824, 2760.082914, 2759.711827, 2760.840004, 
2761.215128, 2760.84743, 2760.099193, 2760.851808, 2759.359468, 
2758.25031, 2758.624201, 2759.755281, 2760.511577, 2761.635327, 
2763.137948, 2763.143331, 2761.64191, 2763.1528, 2762.406524, 
2763.525491, 2763.157339, 2762.781356, 2763.909695, 2764.666851, 
2763.919813, 2763.543975, 2763.928017, 2765.045866, 2765.426243, 
2766.182961, 2765.058383, 2763.936351, 2765.06542, 2763.947052, 
2764.699019, 2764.70424, 2763.584867, 2763.22145, 2763.975492, 
2764.349189, 2764.354994, 2763.613307, 2760.9942, 2759.878475, 
2762.486297, 2762.115389, 2763.994527, 2762.121615, 2762.876597, 
2761.391148, 2761.391991, 2761.768055, 2761.769126, 2759.525662, 
2758.406516, 2758.786634, 2758.783261, 2757.669758, 2756.920758, 
2756.540737, 2756.545277, 2756.176055, 2756.171515, 2755.423667, 
2754.67611, 2753.178257, 2752.423923, 2753.16895, 2748.678277, 
2747.927494, 2746.429754, 2746.424955, 2746.797971, 2746.424663, 
2745.674982, 2746.424792, 2743.432215, 2743.432994, 2741.179315, 
2742.67553, 2739.672447, 2738.921647, 2738.173637, 2737.424005, 
2736.294822, 2735.180735, 2734.431882, 2734.425007, 2733.301013, 
2732.930623, 2730.676264, 2729.172881, 2728.424855, 2727.298218, 
2726.930066, 2726.177678, 2724.680797, 2723.5587, 2723.931652, 
2722.428675, 2721.683421, 2720.557108, 2718.311893, 2716.438883, 
2717.549484, 2717.545139, 2718.671354, 2714.548217, 2716.42254, 
2713.060416, 2714.557005, 2714.554119, 2712.681174, 2711.561898, 
2710.437904, 2710.438585, 2709.688272, 2707.067122, 2708.192203, 
2705.564778, 2707.439879, 2705.949079, 2705.573079, 2705.577036, 
2704.828393, 2702.954102, 2702.20199, 2701.079406, 2699.206235, 
2697.708138, 2695.836101, 2696.588814, 2696.968721, 2696.966354, 
2696.970764, 2696.22504, 2694.349549, 2695.852121)), .Names = c("Time", 
"Fz"), row.names = 1299:1498, class = "data.frame")

      

+3


source to share


1 answer


Here's a possible approach using the package zoo

Function

library(zoo)
IntrvalsDif <- function(x, n) {
  temp <- rollsum(x, n)
  sapply(seq_len(length(x) - n + 1), function(x) temp[x + n] - temp[x])
}

      



Test

IntrvalsDif(df$Fz, 20)
#   [1]   22.712120   31.780260   37.846728   48.042024   55.623904   59.831923   61.789132   67.470180   73.906584   79.581470
#  [11]   81.889676   83.816224   80.498591   81.303658   85.113723   87.410109   91.579034   93.126436   97.284240   99.556696
#  [21]  101.445531   99.204143   98.458872   96.574869   97.299629  101.391636  108.484213  111.843855  111.830721  108.817601
#  [31]  108.804484  104.667484  103.899693  100.132011   94.484088   91.846433   88.453795   88.053184   85.777665   83.127912
#  [41]   83.855688   84.967116   86.079728   84.951745   81.943294   79.311783   74.809550   70.305436   67.663273   68.031700
#  [51]   68.010152   72.486929   78.453469   83.671546   86.262716   87.723876   86.573048   83.175009   80.529002   79.394259
#  [61]   73.758934   70.745457   66.612867   64.351423   62.484932   55.371569   46.756346   42.618145   38.493014   34.730700
#  [71]   27.612391   18.995970    6.271506   -3.829068  -10.178434  -18.028571  -24.761686  -32.615260  -40.093775  -46.838338
#  [81]  -52.458680  -61.082382  -69.714532  -78.721904  -86.991107  -88.898297  -88.564729  -94.941400 -102.449946 -114.087060
#  [91] -120.115474 -131.764148 -140.435989 -151.731834 -164.906638 -176.581499 -185.261108 -191.700139 -198.144375 -208.693815
# [101] -217.378887 -226.065596 -232.122351 -241.185464 -251.002620 -264.177813 -277.722667 -288.293768 -297.354158 -303.775595
# [111] -313.566873 -314.745817 -316.279034 -317.070287 -318.979261 -322.381222 -328.406668 -334.420050 -342.305322 -345.705694
# [121] -351.343224 -352.849444 -359.231092 -360.347288 -361.457419 -363.321285 -366.306745 -366.670795 -366.308869 -364.077356
# [131] -365.210203 -366.711412 -371.584748 -371.950517 -369.317205 -366.687770 -363.297645 -361.044891 -356.536870 -352.777977
# [141] -349.395261 -347.887047 -344.504089 -340.758605 -337.002095 -329.879165 -319.755890 -313.731643 -310.687084 -311.395920
# [151] -304.987941 -305.325773 -300.807844 -303.409489 -306.756032 -306.724804 -305.584028 -302.559006 -301.033458 -301.379656
# [161] -296.103079          NA          NA          NA          NA          NA          NA          NA          NA          NA
# [171]          NA          NA          NA          NA          NA          NA          NA          NA          NA          NA
# [181]          NA

      

+3


source







All Articles