I am trying to use tidyr
to unpivot a time series dataset that has many variables running across the rows at the top of the file, one variable running down the first column, and observations beginning at some point in the dataset. I can't work out how to achieve this using pivot_longer
or pivot_wider
. Is this possible?
Here is a miniature example of my raw data, with the actual observations beginning at row 4 column 2.
A B C
varname_1 category_1 category_1
varname_2 category_2 category_2
varname_3 category_3 category_4
1990 100 200
1991 101 201
1992 102 202
I want it in a long format so my result would be:
varname_1 varname_2 varname_3 Year Obs
category_1 category_2 category_3 1990 100
category_1 category_2 category_3 1991 101
category_1 category_2 category_3 1992 102
category_1 category_2 category_4 1990 200
category_1 category_2 category_4 1991 201
category_1 category_2 category_4 1992 202
Script to generate my raw data:
my_table = data.frame(
A=c("varname_1","varname_2","varname_3",1990,1991,1992),
B=c("category_1","category_2","category_3",100,101,102),
C=c("category_1","category_2","category_4",200,201,202))
and my desired result:
my_result = data.frame(
varname_1=c("category_1","category_1","category_1","category_1","category_1","category_1"),
varname_2=c("category_2","category_2","category_2","category_2","category_2","category_2"),
varname_3=c("category_3","category_4","category_3","category_4","category_3","category_4"),
Year=c(1990,1991,1992,1990,1991,1992),Obs=c(100,101,102,200,201,202))