获得净值作为从数据帧中的R的比例(第2部分)(Getting net values as a pro

2019-11-04 21:48发布

我最近得到了与帮助计算中的R表的净比例 ,而是试图做出一个总结还没有工作,因为我选择的答案,我要发布一个新的问题。

这里是我的原始数据(我称之为QF):

genre  status  rb  wrb  inn
Fiction  FAILURE  621  66  1347
Fiction  FAILURE  400  46  928
Fiction  FAILURE  238  35  663
Poetry  FAILURE  513  105  1732
Poetry  FAILURE  165  47  393
Poetry  FAILURE  896  193  2350
Love-story  FAILURE  5690  501  8869
Love-story  FAILURE  1284  174  2793
Love-story  FAILURE  7279  715  13852
Love-story  SUCCESS  18150  1734  39635
Poetry  SUCCESS  1988  226  4712
Love-story  SUCCESS  20110  2222  43953
Love-story  SUCCESS  20762  2288  46706
Poetry  SUCCESS  1824  322  3984
Poetry  SUCCESS  1105  148  2751
Adventure  SUCCESS  4675  617  8462
Adventure  SUCCESS  7943  599  17247
Adventure  SUCCESS  7290  601  17774

由于我管理的答案得到它的风格和成功/失败进行总结,像这样(我喜欢跟踪所有的转换,因此多dataframes):

qf2 <- qf %>% group_by(genre,status) %>% summarise_all(sum)

qf3 <- ff2 %>%  as.data.frame()

qf4 <- qf3 %>% mutate(rowSum = rowSums(.[,names(qf3)[3:5]])) %>% 
group_by(genre) %>% 
summarise_at(vars(names(qf3)[3:5]),   
           funs(net = .[status == "SUCCESS"]/rowSum[status == "SUCCESS"] - 
                  .[status == "FAILURE"]/rowSum[status == "FAILURE"] )) %>%
as.data.frame()

不过我想现在给做的就是整体的比例。 但无论我尝试它只是将无法正常工作。 我想我失去了一些东西明显。

我想是的输出:

Sum-FAILURE  0.329241738  0.036265536  0.634492726
Sum-SUCCESS  0.301794636  0.031519501  0.666685863
Net  -0.027447103  -0.004746035  0.032193137

我试图创建得到这个计算是(为RB):

(Sum(success_rb)/(Sum(success_rb)+Sum(success_wrb)+Sum(Success_inn)) -  (Sum(failure_rb)/(Sum(failure_rb)+Sum(failure_wrb)+Sum(failure_inn))

Answer 1:

qf %>% 
  select(-genre)%>%
  group_by(status) %>% 
  summarise_all(sum)%>%
  {.[-1]/rowSums(.[-1])}%>%
  rbind(.[2,]-.[1,])

          rb          wrb        inn
1   0.3292417  0.036265536 0.63449273
2   0.3017946  0.031519501 0.66668586
21 -0.0274471 -0.004746035 0.03219314

library(data.table)
setDT(qf)[,lapply(.SD,sum),status,.SDcols=3:5][,
             .SD/rowSums(.SD),.SDcols=-1][,rbind(.SD,.SD[2]-.SD[1])]
           rb          wrb        inn
1:  0.3292417  0.036265536 0.63449273
2:  0.3017946  0.031519501 0.66668586
3: -0.0274471 -0.004746035 0.03219314


文章来源: Getting net values as a proportion from a dataframe in R (part 2)