Home > Back-end >  Is there a function with which I can find a combination of values in a data set, which has on averag
Is there a function with which I can find a combination of values in a data set, which has on averag

Time:05-08

These are the instructions that are given:

In 2012 and prior to that time, a pizza chain in Australia, Eagle Boys (taken over by Pizza Hut in 2016), ran an advertising campaign in which several claims about the size of their pizzas as well as those of their main competitor, Domino’s, were made. The file pizza.csv contains the data on which they based their campaign. For each of the 250 pizzas taken into consideration, you are provided with the chain the pizza comes from, the type of crust, toppings and the diameter of the pizza (in cm).

The question I have to answer is the following:

What combination of crust type, toppings and chain the pizza comes from has on average the largest pizzas? What combination yields the smallest pizzas?

enter image description here

Second, the same formula can be used in the actually very important aggregate function, which allows you to apply any FUNction to aggregated data.

a <- aggregate(diameter ~ crust   topping   chain, dat, FUN=mean)

In the second step you want those diameters that equal the max and the min.

a[a$diameter == max(a$diameter), ]
#   crust topping chain diameter
# 3     C       K     X 28.21241

a[a$diameter == min(a$diameter), ]
#    crust topping chain diameter
# 18     C       M     Y  26.6717

Data:

n <- 250
dat <- expand.grid(chain=LETTERS[24:25], crust=LETTERS[1:3], topping=LETTERS[11:13])  
dat <- dat[rep(seq_len(nrow(dat)), n/2), ]
set.seed(42)
dat$diameter <- runif(nrow(dat), 25, 30)
dat <- dat[sample(seq_len(nrow(dat)), n), ] 
  •  Tags:  
  • r
  • Related