got a pretty basic question to ask unfortunately, I am trying to use a pivot_wider to make my data into a panel.
variable id reports gp & ge every year, the column t denotes the year. I want a separate variable gp_t and ge_t for every year in the data(i.e. t =[2011 -2013])
When I use pivot_wider, I am getting the right number of columns, but the rows are not collapsing into each other as they have in the past when I have used this function. Can someone please give me a hand? Because I am using t as the "names_from" for 2 different columns, I duplicated it... was this the wrong thing to do?
df$t2<-df$t
df<-df %>%
select(id,t,t2,gp,ge)%>%
group_by(id)%>%
pivot_wider(names_from = "t", names_prefix= "gp_" values_from = "gp")%>%
pivot_wider(names_from = "t2", names_prefix= "ge_" values_from = "ge")
NB: company 01 went out of business in 2013, this needs to be kept visible in the table if possible!
I am currently getting this:
id | gp_2011 | gp_2012 | gp_2013 | ge_2011 | ge_2012 | ge_2013 |
---|---|---|---|---|---|---|
01 | 25 | NA | NA | 12 | NA | NA |
01 | NA | 32 | NA | NA | 22 | NA |
02 | 95 | NA | NA | 62 | NA | NA |
02 | NA | 73 | NA | NA | 41 | NA |
02 | NA | NA | 68 | NA | NA | 55 |
03 | 24 | NA | NA | 16 | NA | NA |
03 | NA | 34 | NA | NA | 22 | NA |
03 | NA | NA | 41 | NA | NA | 20 |
I want this:
id | gp_2011 | gp_2012 | gp_2013 | ge_2011 | ge_2012 | ge_2013 |
---|---|---|---|---|---|---|
01 | 25 | 32 | NA | 12 | 22 | NA |
02 | 95 | 73 | 68 | 62 | 41 | 55 |
03 | 24 | 34 | 41 | 16 | 22 | 20 |
Any help or handy hints greatly appreciated!
Thanks!