Reputation: 9018
I'd like to aggregate my data.
time<- as.POSIXct(c("2014-12-10 00:11:02","2014-12-10 00:11:02","2014-12-10 00:11:03"), tz= "GMT")
shares<-c(1,2,3)
price<-c(48, 48, 49)
dat<-data.frame(time= as.factor(time), price= price, size =shares)
dat
here is what dat looks like:
time price size
1 2014-12-10 00:11:02 48 1
2 2014-12-10 00:11:02 48 2
3 2014-12-10 00:11:03 49 3
now I try to aggregate dat
aggregate(x=dat, by = list( as.factor(dat$size)), formula = cbind(dat$time, dat$price) ~ as.numeric(dat$size), FUN="sum")
I want to aggregate the shares column in dat across time and price so it looks like this:
time price size
2014-12-10 00:11:02 48 3
2014-12-10 00:11:03 49 3
any ideas? Thank you.
Upvotes: 1
Views: 1234
Reputation: 6528
Either
aggregate(x = dat$size, by = list(time, price), FUN = "sum")
Group.1 Group.2 x
1 2014-12-10 00:11:02 48 3
2 2014-12-10 00:11:03 49 3
Or
aggregate(size ~ time + price, data = dat, FUN = "sum")
time price size
1 2014-12-10 00:11:02 48 3
2 2014-12-10 00:11:03 49 3
These correspond to the docs for ?aggregate
, which give possible forms:
aggregate(x, by, FUN, ..., simplify = TRUE)
aggregate(formula, data, FUN, ..., subset, na.action = na.omit)
Your attempt mixed these forms up together.
Upvotes: 3