# ETL
library(tidyverse)14 Create a list
In this exercise, we’re going to learn how to create a list, and to analyze it.
Lists are important in the R language primarily because they are versatile “super” data types capable of storing a collection of diverse objects in a single, ordered container.
15 Create demo data frame
Let’s create a demo data frame with 2 variables :
some products.
some markets where they are distributed.
We are going to create another data frame, using lists, to get a more compact format.
This new data frame will also be very useful to perform quickly some analysis, such as :
getting the list of products sold in some countries.
identifying the markets where some products are sold.
# create vectors
products <- c(rep("item A", 3),
rep("item B", 3),
rep("item C", 3),
rep("item D", 4)
)
markets <- c("Singapore", "Malaysia", "Philippines",
"Singapore", "Malaysia", "Thailand",
"Taiwan", "Malaysia", "Thailand",
"Indonesia", "Malaysia", "Singapore", "Philippines"
)
# create dataframe
df1 <- data.frame(products,
markets)
# display
df1 products markets
1 item A Singapore
2 item A Malaysia
3 item A Philippines
4 item B Singapore
5 item B Malaysia
6 item B Thailand
7 item C Taiwan
8 item C Malaysia
9 item C Thailand
10 item D Indonesia
11 item D Malaysia
12 item D Singapore
13 item D Philippines
16 Create list
We are now going to create a list of markets where is sold each product.
The result is a data frame with :
2 variables : product and list of markets where it is sold.
and only 4 rows.
- the new variable “list_markets” is a list
# aggregate
df1 <- df1 |> group_by(products) |>
summarise(list_markets = list(markets))
glimpse(df1)Rows: 4
Columns: 2
$ products <chr> "item A", "item B", "item C", "item D"
$ list_markets <list> <"Singapore", "Malaysia", "Philippines">, <"Singapore", "…
17 Filter products based on markets
We will use the function map_lgl() from the purrr package.
Its primary function is to apply a specified function to each element of a list or vector and consistently return a logical vector (a vector of TRUE and FALSE values) of the same length as the input.
A common use case is checking properties of elements in a data frame or list.
17.1 Products sold in Singapore
Let’s start with a simple example : to filter the products which are sold in Singapore.
We can write the following code :
# Identify products that contain "Singapore" in list_markets
products_with_singapore <- df1 |>
filter(map_lgl(list_markets, ~ "Singapore" %in% .))
# display
products_with_singapore# A tibble: 3 × 2
products list_markets
<chr> <list>
1 item A <chr [3]>
2 item B <chr [3]>
3 item D <chr [4]>
In this case, we also could use the simple functions filter() and str_detect() that we saw previously.
df1 |> filter(str_detect(list_markets, "Singapore"))# A tibble: 3 × 2
products list_markets
<chr> <list>
1 item A <chr [3]>
2 item B <chr [3]>
3 item D <chr [4]>
17.2 Products sold in Singapore and The Philippines
Now, let’s say that we want to identify the products which are sold in both Singapore and The Philippines.
This time we can use a vector to capture those 2 criterias c("Singapore", "Philippines") as below :
# Identify products that contain both "Singapore" and "Malaysia" in list_markets
products_with_singapore_and_philippines <- df1 |>
filter(map_lgl(list_markets, ~ all(c("Singapore", "Philippines") %in% .)))
# display
products_with_singapore_and_philippines# A tibble: 2 × 2
products list_markets
<chr> <list>
1 item A <chr [3]>
2 item D <chr [4]>