I have a wide data frame I'm trying to convert to long. I also want to capture a new column in the long format.
Been looking at a couple posts but getting stuck.
For example, I have this data:
State NumTotal Num1 Num2 Num3 Num4
A 352 98 10 174 70
B 331 233 42 30 26
I would like this data:
State NumClass Num
A Num1 98
A Num2 10
A Num3 174
A Num4 70
B Num1 233
B Num2 42
B Num3 30
B Num4 26
The only actual output I can get is from this code (with df_so
being the data frame with the data):
long <- df_so %>%
gather(State, NumTotal)
long
which does this:
State NumTotal
Num1 98
Num1 233
Num2 10
Num2 42
Num3 174
Num3 30
Num4 70
Num4 26
So I'm losing the State
value (A
or B
) and know I need to generate a NumClass
column but don't see how.