I have 5 vectors of different lengths
a <- c(1) #with length of 1
b <- c(4.4,3.5) #length 2
c <- c(5.6,7.8,6.0) #length 3
d <- c(0.8,6.9,8.8,5.8) #length 4
e <- c(1.8,2.5,2.3,6.5,1.1) #length is 5
I am trying to get the mean of elements across all vectors:
#since there are 5 values available for 1st element
a[1]+b[1]+c[1]+d[1]+e[1] / 5
#since there are 4 values available for 2nd element
b[2]+c[2]+d[2]+e[2] / 4
#next divide by 3 and 2...1
c[3]+d[3]+e[3] / 3 and so on...
I need the mean of these values in another array so that I can do further processing of the data.
Let
l <- list(a, b, c, d, e)
then do:
tapply(unlist(l), sequence(lengths(l)), mean)
1 2 3 4 5
2.720 5.175 5.700 6.150 1.100
Another approach:
rowMeans(sapply(l, `length<-`, max(lengths(l))), na.rm = TRUE)
[1] 2.720 5.175 5.700 6.150 1.100
colMeans(plyr::rbind.fill.matrix(sapply(l, t)), na.rm = TRUE)
1 2 3 4 5
2.720 5.175 5.700 6.150 1.100