A dataset can be written in two different formats: wide and long.
A wide format has values that do not repeat in the first column.
A long format has values that do repeat in the first column.
Example of wide format:
Example of long format:
We can see that in the wide format there is no repetitive value in the first column.
Sometimes when we download the datasets of interest from the website, they are not necessarily ready for statistical analysis. Thus, we will see how to transform between these two formats in R.
First we load the data.
This data is in the wide format.
Now we reshape this dataset.
data1= reshape(data = rw, idvar= "Country.Name", varying = 2:11, #We need to specify here the columns to be reshaped sep= "", timevar= "year", times = c(2017,2018,2019,2020,2021), new.row.names= 1:10000, direction = "long")
Now we can see it's in the long format now.
Now we load this dataset in the long format.
#data source: World Bank (WDI) 2007-2021
rl.wide= reshape(data = rl, idvar= "year", v.names= c("GDP"), timevar= "country", direction = "wide")
Now we can see that it has been transformed into a wide format.