data(Produc, package = "plm")Tasks
Important
- Try this without AI first - struggle builds understanding.
- Ask if you are struggling. Asking builds understanding as well!
Task 1
Import the Produc dataset (from the package plm) using the following code:
Task 2
Convert the data from long to wide
Sample Solution
library(plm)
library(dplyr)
library(tidyr)
library(tibble)
data("Produc", package = "plm")
Produc_wide <- Produc |>
select(year, state, unemp) |>
pivot_wider(names_from = state, values_from = unemp) |>
column_to_rownames("year")Task 3
Convert the data to a matrix
Sample Solution
Produc_matrix <- as.matrix(Produc_wide)Task 4
Calculate mean unemployment first using the wide dataframe, then the new matrix. Compare the speed difference.
Sample Solution
library(bench)
bm <- bench::mark(
matrix = mean(Produc_matrix, na.rm = TRUE),
dataframe = mean(Produc_wide |> unlist(),na.rm = TRUE),
filter_gc = FALSE
)
bm$total_time[2]/bm$total_time[1][1] 5.71s