tidy_data_2 <- read_csv(file = "data/untidy_data.csv",
show_col_types = FALSE) %>%
pivot_longer(
cols = itemsprice_2018:totalprice_2020,
names_to = c("category", "year"),
names_sep = "_",
values_to = "value",
names_transform = list(year = as.integer),
values_transform = list(value = as.character)
) %>%
pivot_wider(
id_cols = c(customer_id, year),
names_from = category,
values_from = value
) %>%
separate(
col = itemsprice,
into = c("items", "price_per_item"),
sep = " ",
remove = TRUE,
convert = TRUE
) %>%
mutate(
price_per_item = stringr::str_replace_all(
string = price_per_item,
pattern = "[()]",
replacement = ""
)
) %>%
type_convert(
trim_ws = TRUE
)