# NOT RUN {
# To convert data frame to tidy data (long) format, run:
library(dplyr)
library(tidyr)
library(stringr)
use <- drug_use %>%
select(age, n, ends_with("_use")) %>%
pivot_longer(-c(age, n), names_to = "drug", values_to = "use") %>%
mutate(drug = str_sub(drug, start=1, end=-5))
freq <- drug_use %>%
select(age, n, ends_with("_freq")) %>%
pivot_longer(-c(age, n), names_to = "drug", values_to = "freq") %>%
mutate(drug = str_sub(drug, start=1, end=-6))
drug_use_tidy <- left_join(x=use, y=freq, by = c("age", "n", "drug")) %>%
arrange(age)
# }
Run the code above in your browser using DataLab