-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate.r
More file actions
21 lines (15 loc) · 816 Bytes
/
aggregate.r
File metadata and controls
21 lines (15 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# aggregation same as SQL.
require(ggplot2)
data(diamonds)
mean(diamonds$price) # this will calculate the average price of all diamonds.
# in order to calculate mean price / per cut type, we need aggregate function.
# the aggregate(statrs with a function price versus cut)
aggregate(price ~ cut, diamonds, mean) # which meens, take the data, break it by cut,
# and then calculate the mean.
# if there is missing data:
aggregate(price ~ cut , diamonds, mean, na.rm = TRUE)
# if we want to see the mean price per cut AND color:
aggregate(price ~ cut + color , diamonds, mean, na.rm = TRUE)
# price and carat per cut:
aggregate(cbind(price, carat) ~ cut , diamonds, mean, na.rm = TRUE)
aggregate(cbind(price, carat) ~ cut + color , diamonds, mean, na.rm = TRUE)