9.7 Exercise 4
Let’s do a small exercise to practice this conversion.
- Read in file DataViz_source_files-main/files/stats_countries_barcelona_2013-2023.csv
correction
2. Columns 2013 to 2022 contain observations. Convert this wide format into a long format. Save into a new object.
correction
# Option 1: list all columns
stats_countries_long <- pivot_longer(stats_countries,
cols=c("2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023"),
values_to = "Population",
names_to = "Years")
# Option 2: select columns that start with "20"
stats_countries_long <- pivot_longer(stats_countries,
cols=starts_with("20"),
values_to = "Population",
names_to = "Years")
# Option 3: select all numeric columns
stats_countries_long <- pivot_longer(stats_countries,
cols=where(is.numeric),
values_to = "Population",
names_to = "Years")
3. Do the 3 following tasks linked by a %>% (pipe):
- Keep only rows that
- match continent “America”
- have a population >= 50
- Remove column Continent.
- Produce a barplot with “Years” in the x-axis, Population in the y-axis, and split by Country.