I have a data.frame
similar to:
> df<-data.frame(ID=1:5, Age30_40=c(1,0,0,1,0),Age40_50=c(0,1,1,0,0), Age50_60=c(0,0,0,0,1))
> df
ID Age30_40 Age40_50 Age50_60
1 1 1 0 0
2 2 0 1 0
3 3 0 1 0
4 4 1 0 0
5 5 0 0 1
I want to convert this wide data to a long data such that my output is as follows:
ID AgeRange
1 Age30_40
2 Age40_50
3 Age40_50
4 Age30_40
5 Age50_60
However, all that I could manage thus far is this:
> library(tidyr)
> gather(df, AgeRange, InThisRange, Age30_40:Age50_60)
ID AgeRange InThisRange
1 1 Age30_40 1
2 2 Age30_40 0
3 3 Age30_40 0
4 4 Age30_40 1
5 5 Age30_40 0
6 1 Age40_50 0
7 2 Age40_50 1
8 3 Age40_50 1
9 4 Age40_50 0
10 5 Age40_50 0
11 1 Age50_60 0
12 2 Age50_60 0
13 3 Age50_60 0
14 4 Age50_60 0
15 5 Age50_60 1
Since this is a simplified example, I can loop through each entry and keep the only non-zero AgeRange
entry corresponding to each ID by checking the boolean variable InThisRange
. However, this is definitely not a very efficient solution for large data frames. Is there a simpler way to achieve this?