LEGO

Packages

We’ll use the tidyverse package for much of the data wrangling and visualization. The tidyverse package should be installed by now on your computer. If not, take again a look at the Set Up tutorial. You can load the tidyverse with the following command:

library(tidyverse)

Data

The data is contained in the file lego.csv and can be found on Moodle in the folder Data files, Quarto files and Solutions to previous problem sets.

It is simulated data based on real prices for LEGO sales in 2018 in the United States. Names and hobbies have been randomly generated. The dataset contains the following variables.

Variable Description
first_name First name of customer
last_name Last name of customer
age Age of customer
phone_number Phone number of customer
set_id Set ID of LEGO set purchased
number Item number of LEGO set purchased
theme Theme of LEGO set purchased
subtheme Sub theme of LEGO set purchased
year Year of purchase
name Name of LEGO set purchased
pieces Number of pieces of LEGOs in set purchased
us_price Price of the set purchased in US Dollars
quantity Quantity of LEGO set(s) purchased

Solutions

Solution 1. Save the data and the starter file appropriately so the following code works.

lego <- read_csv("data/lego.csv")

Remark: Don’t forget to remove eval: false or change it to eval: true.

Take a glimpse at the data.

lego
# A tibble: 620 × 13
   first_name last_name      age phone_number set_id number theme subtheme  year
   <chr>      <chr>        <dbl> <chr>         <dbl> <chr>  <chr> <chr>    <dbl>
 1 Kimberly   Beckstead       24 216-555-2549  24701 76062  DC C… Mighty …  2018
 2 Neel       Garvin          35 819-555-3189  25626 70595  Ninj… Rise of…  2018
 3 Neel       Garvin          35 819-555-3189  24665 21031  Arch… <NA>      2018
 4 Chelsea    Bouchard        41 <NA>          24695 31048  Crea… <NA>      2018
 5 Chelsea    Bouchard        41 <NA>          25626 70595  Ninj… Rise of…  2018
 6 Chelsea    Bouchard        41 <NA>          24721 10831  Duplo <NA>      2018
 7 Bryanna    Welsh           19 <NA>          24797 75138  Star… Episode…  2018
 8 Bryanna    Welsh           19 <NA>          24701 76062  DC C… Mighty …  2018
 9 Caleb      Garcia-Wide…    37 907-555-9236  24730 41115  Frie… <NA>      2018
10 Caleb      Garcia-Wide…    37 907-555-9236  25611 21127  Mine… Minifig…  2018
# ℹ 610 more rows
# ℹ 4 more variables: name <chr>, pieces <dbl>, us_price <dbl>, quantity <dbl>

Solution 2. In this sample, the first three common names of purchasers are “Jackson”, “Jacob” and “Joseph”.

lego |> 
  count(first_name) |>
    arrange(desc(n))
# A tibble: 211 × 2
   first_name     n
   <chr>      <int>
 1 Jackson       13
 2 Jacob         11
 3 Joseph        11
 4 Michael       10
 5 Audrey         8
 6 Connor         8
 7 Kaitlyn        8
 8 Lucas          8
 9 Amanda         7
10 Joshua         7
# ℹ 201 more rows

Solution 3. Dropping the observations with NA subtheme:

lego <- lego |> 
  drop_na(subtheme)

REMARK: Use the reduced dataset to answer the following questions.

Solution 4. The three most common themes are “Star Wars”, “Mixels” and “Gear”.

lego |> 
  group_by(theme) |>
  mutate(quantity = sum(quantity)) |>
  distinct(theme, quantity) |>
  arrange(desc(quantity))
# A tibble: 19 × 2
# Groups:   theme [19]
   theme                   quantity
   <chr>                      <dbl>
 1 Star Wars                    117
 2 Mixels                        83
 3 Gear                          79
 4 City                          59
 5 Ninjago                       48
 6 Bionicle                      46
 7 Friends                       37
 8 DC Comics Super Heroes        35
 9 Nexo Knights                  32
10 Marvel Super Heroes           30
11 Dimensions                    24
12 Disney Princess               20
13 Minecraft                     10
14 Architecture                  10
15 Advanced Models                5
16 Duplo                          5
17 Seasonal                       4
18 Ghostbusters                   3
19 Collectable Minifigures        3

Within the theme “Mixels”, the most common subtheme is “Series 7”; within the theme “Star Wars”, the most common subtheme is “The Force Awakens”; within the theme “Gear”, the most common subtheme is “Tole-Play toys”.

lego |>
  filter(theme %in% c("Star Wars", "Mixels", "Gear")) |>
  group_by(subtheme) |>
  mutate(quantity = sum(quantity)) |>
  distinct(theme, subtheme, quantity) |> 
  arrange(desc(quantity))
# A tibble: 28 × 3
# Groups:   subtheme [28]
   theme     subtheme          quantity
   <chr>     <chr>                <dbl>
 1 Mixels    Series 7                30
 2 Mixels    Series 8                28
 3 Mixels    Series 9                25
 4 Star Wars The Force Awakens       21
 5 Star Wars Buildable Figures       18
 6 Star Wars Episode V               15
 7 Star Wars Battlefront             12
 8 Star Wars MicroFighters           11
 9 Gear      Role-Play toys          11
10 Gear      Stationery              11
# ℹ 18 more rows

Solution 5. Using the case_when() function, a new variable can be added. The case_when() function evaluates the arguments in order; here I have chosen to start with the highest age and let the function work the way through to the youngest age.

lego <- lego |> 
  mutate(
    age_group = 
      case_when(
      age >= 51 ~ "51 and over", 
      age >= 36 ~ "36 - 50",
      age >= 26 ~ "26 - 35",
      age >= 19 ~ "19 - 25",
      age <= 18 ~ "18 and under"
    )
  ) 

Solution 6. The age group “36-50” purchased the most lego sets, followed by the age age group “26-35” and “19-25”.

lego |> 
  group_by(age_group) |>
  mutate(quantity = sum(quantity)) |>
  distinct(age_group, quantity) |> 
  arrange(desc(quantity))
# A tibble: 5 × 2
# Groups:   age_group [5]
  age_group    quantity
  <chr>           <dbl>
1 36 - 50           232
2 26 - 35           193
3 19 - 25           119
4 51 and over        73
5 18 and under       33

Solution 7. Replicaating the plot:

lego |> 
  filter(theme %in% c("Nexo Knights", "City", "Ninjago")) |> 
  ggplot(aes(x = theme, fill = age_group)) +
    geom_bar() +
    scale_fill_brewer(palette = "Set1") +
    labs(
      title = "Distribution of age within each of the three popular themes",
      x = "Popular LEGO theme",
      fill = "Age group"
      )

Between the variables theme and age_group there seems to be some association (at least within this sample). If you adjust the position of the bar chart to “fill”, you can observe the different proportions of age groups within each theme. The proportion of “51 and over”s purchasing Ninjago sets is e.g. bigger than the same group purchasing City sets.

Solution 8. The age group 36-50 has spent the most money on lego with 9532.87 dollars.

lego |>
  mutate(money_spent = quantity * us_price) |>
  group_by(age_group) |>
  summarise(age_group_spending = sum(money_spent)) |>
  arrange(desc(age_group_spending))
# A tibble: 5 × 2
  age_group    age_group_spending
  <chr>                     <dbl>
1 36 - 50                   7064.
2 26 - 35                   4872.
3 19 - 25                   3377.
4 51 and over               1877.
5 18 and under               809.

Solution 9. The result shows 35 rows, thus there are 35 observations from the age group 36-50 that have been buying either a set from the Star Wars or the Ninjago theme.

lego |>
  filter(age_group == "36 - 50", theme == "Star Wars" | theme == "Ninjago")
# A tibble: 35 × 14
   first_name last_name   age phone_number set_id number theme    subtheme  year
   <chr>      <chr>     <dbl> <chr>         <dbl> <chr>  <chr>    <chr>    <dbl>
 1 Chelsea    Bouchard     41 <NA>          25626 70595  Ninjago  Rise of…  2018
 2 Toni       Borison      40 284-555-4560  25626 70595  Ninjago  Rise of…  2018
 3 Amanda     Tronnier     45 317-555-7477  24959 75139  Star Wa… The For…  2018
 4 Erik       Brownrigg    44 <NA>          25621 70589  Ninjago  Rise of…  2018
 5 Cecilia    Mauro        50 <NA>          25921 75157  Star Wa… Rebels    2018
 6 Connor     Padilla      36 441-555-6534  24797 75138  Star Wa… Episode…  2018
 7 Caroline   Holstein     38 318-555-7894  24798 75140  Star Wa… The For…  2018
 8 Caroline   Holstein     38 318-555-7894  24959 75139  Star Wa… The For…  2018
 9 Taylor     Allen        41 760-555-8123  25923 75147  Star Wa… Origina…  2018
10 Katherine  Beck         45 209-555-7105  24959 75139  Star Wa… The For…  2018
# ℹ 25 more rows
# ℹ 5 more variables: name <chr>, pieces <dbl>, us_price <dbl>, quantity <dbl>,
#   age_group <chr>
library(knitr)