Aggregate function in r is not working on my dataset -


sample dataset

date   playerid    revenue promo   dayofweek 01/01/2017  146123  0   b   sunday 01/01/2017  219378  0   b   sunday 01/01/2017  198614  0   b   sunday 02/01/2017  292640  30    monday 02/01/2017  139562  10    monday 02/01/2017  124967  20    monday 02/01/2017  107954  20    monday 03/01/2017  28391   10  b   tuesday 03/01/2017  184388  21  b   tuesday 03/01/2017  264222  20  b   tuesday 03/01/2017  184857  0   b   tuesday 04/01/2017  79788   40    wednesday 

i wanted aggregate table dayofweek, , sum revenue each day of week, count number of players using playerid such final output looks this:

  players revenue promo   dayofweek     3      0      b       sunday     4     80            monday     4     51      b       tuesday     1     40            wednesday 

i have been trying aggregate dataset attached above attempts unsuccessful. can help, please?

here code below.

aggdata <-aggregate(mydata, by=list(dayofweek,revenue, promo, playerid),                      fun=sum, na.rm=true) 

i got following errors

error in fun(x[[i]], ...) : invalid 'type' (character) of argument 

dplyr approach

library(dplyr) ans <- df %>%   group_by(dayofweek) %>%   summarise(promo=unique(promo), revenue=sum(revenue), playerid=n()) 

output

  dayofweek promo revenue playerid       <chr> <chr>   <int>    <int> 1    monday          80        4 2    sunday     b       0        3 3   tuesday     b      51        4 4 wednesday          40        1 

data

df <- structure(list(date = c("01/01/2017", "01/01/2017", "01/01/2017",  "02/01/2017", "02/01/2017", "02/01/2017", "02/01/2017", "03/01/2017",  "03/01/2017", "03/01/2017", "03/01/2017", "04/01/2017"), playerid = c(146123l,  219378l, 198614l, 292640l, 139562l, 124967l, 107954l, 28391l,  184388l, 264222l, 184857l, 79788l), revenue = c(0l, 0l, 0l, 30l,  10l, 20l, 20l, 10l, 21l, 20l, 0l, 40l), promo = c("b", "b", "b",  "a", "a", "a", "a", "b", "b", "b", "b", "a"), dayofweek = c("sunday",  "sunday", "sunday", "monday", "monday", "monday", "monday", "tuesday",  "tuesday", "tuesday", "tuesday", "wednesday")), .names = c("date",  "playerid", "revenue", "promo", "dayofweek"), row.names = c(na,  -12l), class = c("data.table", "data.frame")) 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -