Reputation: 76
I have the following data.
I'm trying to find the best way to visualise this data in a table format (in R) but I can't seem to find an option that is easy to read or makes comparison easy. Any suggestions?
Upvotes: 0
Views: 409
Reputation: 18683
If the data is in df1, the following command reshapes it to a wide format:
library(tidyr)
library(dplyr)
df1 %>%
pivot_wider(id_cols = Country,
names_from = Year,
values_from=c(Score, GDP))
# A tibble: 2 x 11
Country Score_2015 Score_2016 Score_2017 Score_2018 Score_2019 GDP_2015 GDP_2016 GDP_2017 GDP_2018 GDP_2019
<fct> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 Afghan 1 3 5 7 9 101 103 105 107 109
2 Swiss 2 4 6 8 10 102 104 106 108 110
Which looks quite wide, but with better formatting, can be made to look like this:
Data:
df1 <- data.frame(Country=rep(c("Afghan","Swiss"),5),
Year=rep(2015:2019, each=2),
Score=c(1:10), GDP=101:110)
Upvotes: 1