0

Any suggetions to transform a dataframe from this format to a longer format would be wonderful. Original data has 50 names and hundreds of YearMonths. So it should be a pivot from wide to long (?).

df<-data.frame(name=c("Amber","Thomas","Stefan","Joe"),
                      YM_202006=c(8,2,NA,7),
               YM_202007=c(NA,4,1,7),
               YM_202008=c(1,2,3,4))

Before transformation:

enter image description here

After transformation / Desired Output (df_new):

enter image description here

Desired output is faked with this code:

df_new<- data.frame(YM=c("YM_202006","YM_202007","YM_202008"),
                    Amber=c(8,NA,1),
                    Thomas=c(2,4,2),
                    Stefan=c(NA,1,3),
                    Joe=c(7,7,4))

I tried a lot of approaches without getting the desired output.

Hope somebody can help, thx in advance!

0 Answers0