在R中创建子组权重

时间:2016-10-02 22:32:31

标签: r frequency

我目前在R中有以下表/ csv:

Name    Value   Sector  Date
Company1    100 Financials  3/31/2015
Company2    100 Energy  3/31/2015
Company3    100 Healthcare  3/31/2015
Company4    100 Financials  3/31/2015
Company5    100 Energy  3/31/2015
Company6    100 Healthcare  3/31/2015
Company1    100 Financials  6/30/2015
Company2    200 Energy  6/30/2015
Company3    200 Healthcare  6/30/2015
Company4    200 Financials  6/30/2015
Company5    200 Energy  6/30/2015

我想要创建的是每个季度结束日期,每个扇区的权重基于值。

我一直在使用:

cdata <- ddply(Test.Exposure, c("Date", "Sector"), summarise,
               Sumx1 = sum(Value))

这给了我:

            Date     Sector Sumx1
1      3/31/2015     Energy   200
2      3/31/2015 Financials   200
3      3/31/2015 Healthcare   200
4      6/30/2015     Energy   400
5      6/30/2015 Financials   300
6      6/30/2015 Healthcare   400

1)有没有办法获得总和的%

2)是否可以在每个季度结束日期只显示一行,并将每个部门放在一列中,如下所示:

         Financials Energy  Healthcare
3/31/2015   33,33%  33,33%  33,33%
6/30/2015   ... ... ...

2 个答案:

答案 0 :(得分:0)

您可以使用xtabs之后的rowSums

a <- xtabs(Sumx1~Date+Sector, d)

#           Sector
#Date        Energy Financials Healthcare
#  3/31/2015    200        200        200
#  6/30/2015    400        300        400

round(a/rowSums(a)*100, 2)

#           Sector
#Date        Energy Financials Healthcare
#  3/31/2015  33.33      33.33      33.33
#  6/30/2015  36.36      27.27      36.36

数据

d <- read.table(text="            Date     Sector Sumx1
1      3/31/2015     Energy   200
2      3/31/2015 Financials   200
3      3/31/2015 Healthcare   200
4      6/30/2015     Energy   400
5      6/30/2015 Financials   300
6      6/30/2015 Healthcare   400", header=T)

答案 1 :(得分:0)

我们可以使用dplyr/tidyr

执行此操作
library(dplyr)
library(tidyr)
Test.Exposure %>% 
    group_by(Date, Sector) %>% 
    summarise(Sumx1 = sum(Value)) %>% 
    group_by(Date) %>%
    mutate(Sumx1 = round(100*Sumx1/sum(Sumx1),2)) %>% 
    spread(Sector, Sumx1)
#       Date Energy Financials Healthcare
#      <chr>  <dbl>      <dbl>      <dbl>    
#1 3/31/2015  33.33      33.33      33.33
#2 6/30/2015  36.36      27.27      36.36