1

I have a data frame that looks like this: enter image description here

And I want to make it so that my output will be:

The "Year" Column will have values of 2001,2002,2003.....

The "Values" Column will have the values from the respective years

Apologies in advance, I'm new to R studio and I've been stuck on this question for a long time.

Maël
  • 45,206
  • 3
  • 29
  • 67
Bryan Hii
  • 129
  • 7
  • You should use e.g. `tidyr::pivot_longer` – Maël Sep 02 '22 at 08:31
  • @Maël Lemme try to understand the dplyr method first, because my lecturer hasn't taught me anything about reshape yet. Thanks for the suggestion. – Bryan Hii Sep 02 '22 at 08:38
  • @Bryan the most up to date way you a to use https://stackoverflow.com/a/57939670/5784757 - it’s a tidyr way not a dplyr way. – user438383 Sep 02 '22 at 09:11

1 Answers1

0

I have found the answer thanks to @Mael Reshaping data.frame from wide to long format

I did it by using the pivot_longer function

library(tidyr)
pivot_longer(data= df4,
             cols = c("2001":"2050"),
             values_to = "Values"
             names_to = "Year")
Bryan Hii
  • 129
  • 7