0

I am trying to convert a dataframe from wide to long

Currently, the dataframe is set up as:

ID Value1 Value2 Value3
1 xxx yyy zzz
2 xxx yyy NA
3 xxx yyy NA
4 xxx NA NA
5 xxx yyy zzz

I'd like it to look like this:

ID Values
1 xxx
1 yyy
1 zzz
2 xxx
2 yyy
3 xxx
3 yyy
4 xxx
5 xxx
5 yyy
5 zzz

Any advice on how to do this?

I have tried to find code to convert from wide to long or transpose all columns into one, but am having trouble figuring this out. Thanks!

  • Hi Leah, you probably need `pivot_longer` from the `tidyr` package. Please check this [cheat sheet](https://github.com/rstudio/cheatsheets/blob/main/tidyr.pdf). And welcome to SO! – Pavel Filatov Feb 11 '23 at 23:51
  • `df %>% pivot_longer(-ID) %>% select(-name) %>% drop_na()` – Chamkrai Feb 12 '23 at 00:00

0 Answers0