I have data organized by country-year, with a ID for a dyadic relationship. I want to organize this by dyad-year.
Here is how my data is organized:
We can reshape from 'long' to 'wide' using different methods. Two are described below.
Using 'data.table', we convert the 'data.frame', to 'data.table' (setDT(mydf)
), create a sequence column ('ind'), grouped by 'dyadic_id' and 'year'. Then, we convert the dataset from 'long' to 'wide' format using dcast
.
library(data.table)
setDT(mydf)[, ind:= 1:.N, by = .(dyadic_id, year)]
dcast(mydf, dyadic_id+year~ paste('country_codes', ind, sep='_'), value.var='country_codes')
# dyadic_id year country_codes_1 country_codes_2
#1: 1 1990 200 20
#2: 1 1991 200 20
#3: 2 1990 300 10
#4: 3 1990 100 10
#5: 4 1991 500 200
Or using dplyr/tidyr
, we do the same i.e. grouping by 'dyadic_id', 'year', create a 'ind' column (mutate(...
), and use spread
from tidyr
to reshape to 'wide' format.
library(dplyr)
library(tidyr)
mydf %>%
group_by(dyadic_id, year) %>%
mutate(ind= paste0('country_codes', row_number())) %>%
spread(ind, country_codes)
# dyadic_id year country_codes1 country_codes2
# (dbl) (dbl) (dbl) (dbl)
#1 1 1990 200 20
#2 1 1991 200 20
#3 2 1990 300 10
#4 3 1990 100 10
#5 4 1991 500 200