Mike Dude
Mike Dude

Reputation: 151

total column based on month in r

I am trying to summarize data based on month. For example, I have this data set:

x
        Date App Vol
1 2010-01-30   A 100
2 2010-01-28   B 140
3 2010-01-30   C 160
4 2010-02-28   A 110
5 2010-02-28   B 120
6 2010-02-28   C 300

I would like to be able to summary App data by each month. According to the data frame above, A should be 210, B = 260, C=460 etc.

I am using aggregate function ase below, but getting errors:

y<-aggregate(x$Vol, list(Month = format(as.POSIXct(x$Date), "%Y-%m")), sum)

any ideas?

Upvotes: 0

Views: 559

Answers (2)

Tyler Rinker
Tyler Rinker

Reputation: 109844

Start with turning Vol into numeric (it got messed up somehow):

x$Vol <- as.numeric(as.character(x$Vol))

I can reproduce your eror by turning Vol into a factor as seen here:

x$Vol <- as.factor(x$Vol)
aggregate(x$Vol, list(x$App), sum)

#> aggregate(x$Vol, list(x$App), sum)
#Error in Summary.factor(1:2, na.rm = FALSE) : 
#  sum not meaningful for factors

Also you say:

I would like to be able to summary App data by each month. According to the 
data frame above, A should be 210, B = 260, C=460 etc.

If this is the case use:

x$Month <-  format(as.POSIXct(x$Date), "%Y-%m")
aggregate(x$Vol, list(x$Month, x$App), sum)

Otherwise use ttmacer's suggestion.

Upvotes: 1

shhhhimhuntingrabbits
shhhhimhuntingrabbits

Reputation: 7475

x<-read.table(header=T,text="Date       App   Vol
    1 2010-01-30   A        100
    2 2010-01-28   B     140
    3 2010-01-30   C     160
    4 2010-02-28   A        110     
    5 2010-02-28   B         120      
    6 2010-02-28   C         300") 



y<-aggregate(x$Vol, list(Month = format(as.POSIXct(x$Date), "%Y-%m")), sum)
y<-aggregate(x$Vol, list(x$App), sum)

try using this data.

Upvotes: 0

Related Questions