Вопрос

I have numeric vectors with different lengths, ranging from 300 to 500. I would like to 'normalize' them to a length of 100, i.e. for a vector of length 300 I take the mean of 3 values, for a vector of length 500 the mean of 5 values and so on.

How can I bin numeric vectors and calculate the mean without reordering? I have not been successful with cut so far.

# numeric vectors of different lengths
v1 = rnorm(300)
v2 = rnorm(500)

# goal: numeric vectors of same length
v1.binned = c(mean(v1[1],v1[2],v1[3]), ...)
v2.binned = c(mean(v2[1],v2[2],v2[3], v2[4], v2[5]), ...)
Это было полезно?

Решение

You can convert the vectors to a matrix and use colMeans:

colMeans(matrix(v1,100))
[1] -0.09583398  0.01330998  0.11107002
colMeans(matrix(v2,100))
[1] -0.02396420  0.08638535 -0.03953273  0.09861287  0.01112838

Though beware of recycling if the cut size is not an exact multiple of the vector size. In which case, a split-sapply strategy will do the job:

sapply(split(v1,(seq_along(v1)-1)%/%200),mean)
        0         1 
-0.041262  0.111070 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top