Yoki
Yoki

Reputation: 873

Customized aggregation function with more than 1 input in dcast

Is there anyway to use aggregate function with more than one input in dcast, or other functions can do this?

For example

names(airquality) <- tolower(names(airquality))
aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)

dcast(aqm, month ~ variable, mean)

How can I calculate with mean of "value" weighted by day? Thanks in advance.

Upvotes: 1

Views: 113

Answers (1)

Yoki
Yoki

Reputation: 873

As akrun suggested, I checked the answer to other question and it worked:

> aqm %>%
+     select(month, day, variable, value) %>%
+     group_by(month, variable) %>%
+     summarise(weight_avg = weighted.mean(value,day)) %>%
+     spread(variable, weight_avg)

Upvotes: 1

Related Questions