计算在一列的同一组内

时间:2017-10-16 01:03:09

标签: r dataframe

我有一个如下数据框:

names <- c("a", "b", "c","d","e","f", "g","h","i","j")
group_no <- c("A", "B", "C","D","E","D", "C","F","F","C")
amount <- c(10,10,3,6,10,4,6,2,8,1)

data.frame(names, group_no, amount)

我想计算同一组名的百分比。

names <- c("a", "b", "c","d","e","f", "g","h","i","j")
group_no <- c("A", "B", "C","D","E","D", "C","F","F","C")
amount <- c(1,1,0.3,0.6,1,0.4,0.6,0.2,0.8,0.1)
out <- data.frame(names, group_no, amount)
out

2 个答案:

答案 0 :(得分:1)

以下是使用dplyr包的解决方案:

library(dplyr)

test %>% 
  group_by(group_no) %>% 
  mutate(prop = amount/sum(amount))

Source: local data frame [10 x 4]
Groups: group_no [6]

    names group_no amount  prop
   <fctr>   <fctr>  <dbl> <dbl>
1       a        A     10   1.0
2       b        B     10   1.0
3       c        C      3   0.3
4       d        D      6   0.6
5       e        E     10   1.0
6       f        D      4   0.4
7       g        C      6   0.6
8       h        F      2   0.2
9       i        F      8   0.8
10      j        C      1   0.1

答案 1 :(得分:1)

我们可以使用ave

中的base R
test$prop <- with(test, amount/ave(amount, group_no, FUN = sum))
test$prop
#[1] 1.0 1.0 0.3 0.6 1.0 0.4 0.6 0.2 0.8 0.1