[英]Weighted Mean in R
I have a time-series data with different lengths - a,b,c,d,e我有不同长度的时间序列数据 - a,b,c,d,e
#printing a
Time Series:
Start = 1
End = 42
Frequency = 1
[1] 60
#printing b
Time Series:
Start = 1
End = 42
Frequency = 1
[1] 50 70
#printing c
Time Series:
Start = 1
End = 42
Frequency = 1
[1] 40 70 100
#and so on
I am trying to get the mean of elements in all lists: #since there are 5 values available for 1st element我正在尝试获取所有列表中元素的平均值:#since there are 5 values available for 1st element
mean1 <- a[1]+b[1]+c[1]+d[1]+e[1] / 5
#since there are 4 values available for 2nd element
mean2<- b[2]+c[2]+d[2]+e[2] / 4
#next divide by 3 and 2...1
mean3<- c[3]+d[3]+e[3] / 3 and so on...
I need the mean of these values so that I can make a weighted mean for each element for further processing.我需要这些值的平均值,以便我可以为每个元素制作加权平均值以进行进一步处理。 Can anyone give suggestion on what to do to obtain the weighted mean from this data??
任何人都可以就如何从这些数据中获得加权平均值提出建议吗?
We get the objects in a list
, then get the mean
from the list
and do the weighted calculation我们获取
list
中的对象,然后从list
中获取mean
并进行加权计算
lst1 <- lapply(mget(letters[1:5]), unlist)
mx <- max(lengths(lst1))
lst2 <- lapply(lst1, `length<-`, mx)
(1/mx) * (rowSums(mapply(`*`, lst2,
rowMeans(simplify2array(lst2), na.rm = TRUE)), na.rm = TRUE))
you could do:你可以这样做:
l <- c(a,b,c,d,e)
tapply(unlist(l), sequence(lengths(l)), mean)
1 2 3 4 5
6923.783 -2462.537 16402.663 62.005 432.800
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.