I have a function f
that needs to be applied to a single column of length n
in segments of m
length, where m
divides n
. (For example, to a column of 1000 values, apply f
to the first 250 values, then to 250-500, ...).
A loop is overkill, since the column has over 16 million values. I was thinking the efficient way would be to separate the column of length n
into q
vectors of length m
, where mq = n
. Then I could apply f
simultaneously to all this vectors using some lapply-like functionality. Then I cold join the q
vectors to obtain the transformed version of the column.
Is that the efficient way to go here? If so, what function could decompose a column into q
vectors of equal length and what function should I use to broadcast f
across the q
vectors?
Lastly, although less importantly, what if we wanted to do this to several columns and not just one?
Context
I've programmed a function that computes the power spectrum of an EEG signal (a numeric vector). However, it is bad practice to compute the power spectrum of a whole signal at once. The correct method is to compute it epoch by epoch, in 30 or 5 second segments, and average the spectrum of all those epochs. Hence why I need to apply a function to a column (an EEG signal) by epochs (or segments).