计算R中每个月的百分比

Calculating Percentage with respect to each month in R

我正在考虑计算一个月内每个观察值的百分比值。数据展示如下:-

Var1  Date      Value
1     2011-Aug  100
2     2011-Aug  45
1     2011-Jan  70
2     2011-Jan  95
3     2012-Jan  70
4     2012-Dec  80

期望的输出是:-

Var1  Date      Value  Percentage
1     2011-Aug  100    68.96
2     2011-Aug  45     31.03
1     2011-Jan  70     31.11
2     2011-Jan  95     42.22
3     2012-Jan  60     26.66
4     2012-Dec  80     100

我试过这段代码,但它在百分比列中给了我 100 或 0

dt1<-ddply(dt,c("Var1","Date"), function(d) {data.frame(table(dt$value)/length(dt$value))})

谢谢。

使用dplyr:

library(dplyr)
dta %>% group_by(Date) %>%
        mutate(percentage = Value/sum(Value)*100)

Source: local data frame [6 x 4]
Groups: Date

  Var1     Date Value percentage
1    1 2011-Aug   100   68.96552
2    2 2011-Aug    45   31.03448
3    1 2011-Jan    70   42.42424
4    2 2011-Jan    95   57.57576
5    3 2012-Jan    70  100.00000
6    4 2012-Dec    80  100.00000

使用plyr:

ddply(dta, .(Benchmark, Metric), summarize, mean = mean(Value), sd = sd(Value))

使用data.table

DF <- data.frame(Var1 = c(1,2,1,2,3,4), 
    Date = c(rep('2011-Aug', 2), rep('2011-Jan', 3), '2012-Dec'), 
    Value = c(100, 45, 70, 95, 70, 80))

library(data.table)
DT <- data.table(DF)
(DT[, perc := Value / sum(Value) * 100, by=list(Date)])

   Var1     Date Value      perc
1:    1 2011-Aug   100  68.96552
2:    2 2011-Aug    45  31.03448
3:    1 2011-Jan    70  29.78723
4:    2 2011-Jan    95  40.42553
5:    3 2011-Jan    70  29.78723
6:    4 2012-Dec    80 100.00000