2016-02-05 51 views
-2

計算加權平均我有一個數據幀df每個中的R

df<- structure(list(ID = structure(c(1L, 3L, 5L, 6L, 8L), .Label = c("AU-Tum", 
"AU-Wac", "BE-Bra", "BE-Jal", "BE-Vie", "BR-Cax", "BR-Sa3", "CA-Ca1", 
"CA-Ca2", "CA-Ca3", "CA-Gro", "Ca-Man", "CA-NS1", "CA-NS2", "CA-NS3", 
"CA-NS4", "CA-NS5", "CA-NS6", "CA-NS7", "CA-Oas", "CA-Obs", "CA-Ojp", 
"CA-Qcu", "CA-Qfo", "CA-SF1", "CA-SF2", "CA-SF3", "CA-SJ1", "CA-SJ2", 
"CA-SJ3", "CA-TP1", "CA-TP2", "CA-TP4", "CN-Cha", "CN-Ku1", "CZ-Bk1", 
"De-Bay", "DE-Hai", "DE-Har", "DE-Tha", "DE-Wet", "DK-Sor", "ES-Es1", 
"FI-Hyy", "FI-Sod", "FR-Fon", "FR-Hes", "FR-Lbr", "FR-Pue", "GF-Guy", 
"ID-Pag", "IL-Yat", "IT-Col", "IT-Cpz", "IT-Lav", "IT-Non", "IT-Pt1", 
"IT-Ro1", "IT-Ro2", "IT-Sro", "JP-Tak", "JP-Tef", "JP-Tom", "NL-Loo", 
"PT-Esp", "RU-Fyo", "RU-Zot", "SE-Abi", "SE-Fla", "SE-Nor", "SE-Sk1", 
"SE-Sk2", "SE-St1", "UK-Gri", "UK-Ham", "US-Bar", "US-Blo", "US-Bn1", 
"US-Bn2", "Us-Bn3", "US-Dk3", "US-Fmf", "US-Fwf", "US-Ha1", "US-Ha2", 
"US-Ho1", "US-Ho2", "US-Lph", "US-Me1", "US-Me3", "US-Nc2", "US-NR1", 
"US-Oho", "US-So2", "US-So3", "US-Sp1", "US-Sp2", "US-Sp3", "US-Syv", 
"US-Umb", "US-Wcr", "US-Wi0", "US-Wi1", "US-Wi2", "US-Wi4", "US-Wi8", 
"VU-Coc", "CA-Cbo", "RU-Ab", "RU-Be", "RU-Mix", "TH-Mae"), class = "factor"), 
    a = c(24, 11, 21, 10, 30), b = c(23, 10, 17, 9, 31), c = c(22, 
    9, 16, 8, 27), d = c(21, 8, 15, 7, 24), e = c(20, 9, 14, 
    6, 23), f = c(20, 9, 14, 6, 23)), .Names = c("ID", "a", "b", 
"c", "d", "e", "f"), row.names = c(NA, 5L), class = "data.frame") 

我想以計算加權平均爲每一行這樣:

weighted_mean =(0.05 * A + 0.10 * B + 0.15 * C + 0.3 * d + 0.4 * E + 1 * F)/ 2

有人能幫助我嗎?

+1

任何你已經嘗試過自己嗎?爲什麼它不起作用? – Heroka

回答

2

你可以使用weighted.mean

wt<-c(0.05,0.10,0.15,0.3,0.4,1) 
apply(df[,-1],1,weighted.mean,w=wt) 

# 1  2  3  4  5 
# 20.550 8.950 14.625 6.550 24.025 
+1

你忘劃分由兩個 – Heroka

+0

我不這樣認爲如'(24 * 0.05 + 23 * 0.1 + 22 * 0.15 + 21 * 0.3 + 20 * 0.4 + 20 * 1)/ 2'是等於20.55。我認爲'weighted.mean'自動劃分其條款 – etienne

+0

Etienne是正確的 – SimonB

2

這將是

apply(df[, -1], 1, weighted.mean, w=c(0.05, 0.10, 0.15, 0.3, 0.4, 1)) 
#  1  2  3  4  5 
# 20.550 8.950 14.625 6.550 24.025 
+0

@ lukeA。謝謝,但是這個輸出結果是錯誤的。例如,對於第一行,值應該等於20.55。我想我需要乘以你得到的兩個輸出。 – SimonB

+0

@simonB你是對的,我最後刪除'/ 2'。 – lukeA