获取数据:
test <- data.frame(Code = c("A111", "B111", "C111", "D111", "E111", "A222", "B222", "C222", "D222", "E222" ),
Value = c(5L, 10L, 15L, 20L, 25L, 30L, 35L, 45L, 55L, 60L),stringsAsFactors=FALSE)
使用第一个字母 ( fl
) 为代码创建分组变量:
fl <- substr(test$Code,1,1)
groups <- ifelse(fl %in% c("A","B"), 1,
ifelse(fl %in% c("C","D"), 2,
3))
计算权重,ave
用于计算sum
每个组内的权重。
test$Weight <- test$Value / ave(test$Value,groups,FUN=sum)
test
Code Value Weight
1 A111 5 0.0625000
2 B111 10 0.1250000
3 C111 15 0.1111111
4 D111 20 0.1481481
5 E111 25 0.2941176
6 A222 30 0.3750000
7 B222 35 0.4375000
8 C222 45 0.3333333
9 D222 55 0.4074074
10 E222 60 0.7058824