r - Repeating or looping an argument -



r - Repeating or looping an argument -

i quite familiar r i've been using few years now. unfortunately, not versed in creating functions involve looping or repetition of equation. problem goes follows:

i have vector containing on 1000 values. calculate absolute difference between 2 juxtaposing means of equal size subset of vector.

here an example.

i have vector (vec) of length 8

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

i calculate mean of first 2 values ( 0.12472963, 1.15341289) , obtain absolute difference mean of next 2 values (-1.09662288 -0.73241639), thereafter, working way downwards vector.

in case, can utilize next equation:

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

and incrementally increment each number 1 work way downwards manually until end of vector. obtain next vector.

[1] 1.553591 0.3624149 0.8784722 0.497176 0.005337574

what like, however, have automated routine enables me on long vectors , alter number of values calculate means.

it appears me should relatively simple, not know start.

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

r vector repeating

Comments

Popular posts from this blog

javascript - THREE.js reposition vertices for RingGeometry -

javascript - I need to update the text of a paragraph by inline edit -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -