计算分组数据框中的比例

时间:2016-08-04 12:26:34

标签: r

我有以下数据框:

is_valid

如何使列具有 year owngun N 1 2000 Yes 603 2 2000 No 1231 3 2000 Refused 23 4 2012 Yes 440 5 2012 No 841 6 2012 Refused 24 的每个yearowngun级别的比例?

5 个答案:

答案 0 :(得分:1)

假设您的N已经是您的汇总计数,您可以使用data.table获得比例:

library(data.table)
setDT(df)[,prop:=N/sum(N),by=year]
df

   year  owngun    N       prop
1: 2000     Yes  603 0.32471729
2: 2000      No 1231 0.66289715
3: 2000 Refused   23 0.01238557
4: 2012     Yes  440 0.33716475
5: 2012      No  841 0.64444444
6: 2012 Refused   24 0.01839080

使用plyr的相同方法:

library(plyr)     
df2<-ddply(df,.(year),transform,prop=N/sum(N))

答案 1 :(得分:1)

我们可以使用ave

中的base R
df1$prop <- with(df1, N/ave(N, year, FUN = sum))
df1$prop
#[1] 0.32471729 0.66289715 0.01238557 0.33716475 0.64444444 0.01839080

tapply

的其他选项
with(df1, prop.table(tapply(N, list(year, owngun), FUN = sum), 1))

答案 2 :(得分:0)

这就是你想要的吗?

ndf<-reshape2::dcast(dfr[,-1], owngun ~ year)
ndf$p2000=ndf$`2000`/rowSums(ndf[,-1])
ndf$p2012=ndf$`2012`/rowSums(ndf[,-1])
ndf[c(3,1,2),]

owngun级别的比例

   owngun 2000 2012     p2000     p2012
3     Yes  603  440 0.5781400 0.4216263
1      No 1231  841 0.5941120 0.4057717
2 Refused   23   24 0.4893617 0.5053763

每年的owngun比例

ndf<-reshape2::dcast(dfr[,-1], year ~ owngun)
cbind(year=ndf$year,(100*ndf[,-1]/apply(ndf[,-1], 1, sum))[,c(3,1,2)])

  year      Yes       No  Refused
1 2000 32.47173 66.28971 1.238557
2 2012 33.71648 64.44444 1.839080

答案 3 :(得分:0)

prop.tablextabs可能是一个方便的工具:

library(magrittr)
xtabs(N ~., df) %>% prop.table(1) %>% round(2)

#      owngun
#year     No Refused  Yes
#  2000 0.66    0.01 0.32
#  2012 0.64    0.02 0.34

答案 4 :(得分:0)

 >df

   year  owngun    N
 1 2000     Yes  603
 2 2000      No 1231
 3 2000 Refused   23
 4 2012     Yes  440
 5 2012      No  841
 6 2012 Refused   24

 >library(dplyr)

 > df %>% group_by(year)  %>% mutate(Proportion=N/sum(N))


      year  owngun    N  Proportion
     (int)  (fctr) (int)     (dbl)
  1  2000     Yes   603  0.32471729
  2  2000      No  1231  0.66289715
  3  2000 Refused    23  0.01238557
  4  2012     Yes   440  0.33716475
  5  2012      No   841  0.64444444
  6  2012 Refused    24  0.01839080