我有一个数据框df
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
有人可以帮我解决这个问题吗?
答案 0 :(得分: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
答案 1 :(得分: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