Calculate Percentage for each time series observations per Group in R

前端 未结 2 617
感动是毒
感动是毒 2020-12-07 03:37

New to R, so just my getting head around the data wrangling aspect. Tried looking for a similar question but couldn\'t find it.

I would like to add an additional c

2条回答
  •  一向
    一向 (楼主)
    2020-12-07 03:38

    If df is your data.frame, you can do:

    library(data.table)
    setDT(df)[,percentage:=signif(100*views/sum(views),4),by=date][]
    #   views       date article percentage
    #1:  1578 2015-01-01       A      56.99
    #2:   616 2015-01-01       B      22.25
    #3:   575 2015-01-01       C      20.77
    #4:  1744 2015-01-02       A      59.22
    #5:   541 2015-01-02       B      18.37
    #6:   660 2015-01-02       C      22.41
    #7:  2906 2015-01-03       A      69.55
    #8:   629 2015-01-03       B      15.06
    #9:   643 2015-01-03       C      15.39
    

    Or base R:

    df$percentage = signif(100*with(df, ave(views, date, FUN=function(x) x/sum(x))),4)
    

    Data:

    df = structure(list(views = c(1578L, 616L, 575L, 1744L, 541L, 660L, 
    2906L, 629L, 643L), date = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
    3L, 3L, 3L), .Label = c("2015-01-01", "2015-01-02", "2015-01-03"
    ), class = "factor"), article = structure(c(1L, 2L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L), .Label = c("A", "B", "C"), class = "factor"), 
    percentage = c(56.99, 22.25, 20.77, 59.22, 18.37, 22.41, 
    69.55, 15.06, 15.39)), .Names = c("views", "date", "article", 
    "percentage"), class = "data.frame", row.names = c(NA, -9L))
    

提交回复
热议问题