1

I'm trying to reshape the below data frame into a long format, but I'm not having much luck writing the code using reshape() or melt().

                 pH                         SO4     
Lake  Lat   Long  1976   1977   1978  1981  1976  1977
1     58    7.2   4.59   -9999  4.48  4.63  6.5-  -9999
2     58.1  6.3   4.97   -9999  4.6   4.96  5.5-  -9999
4     58.5  7.9   4.32   4.23   4.4   4.49  4.8   6.5
5     58.6  8.9   4.97   4.74   4.98  5.21  7.4   7.6

I need to create a data frame where each row defines a unique combination of lake and year, columns for each chemical variable, and an additional column with the year of the measurement.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Sarah
  • 13
  • 3
  • Please provide a reproducible example and format your questions. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – yusuzech Sep 11 '19 at 17:09
  • Show the code you tried. Describe exactly where you are getting stuck. – MrFlick Sep 11 '19 at 17:20

1 Answers1

-1

the melt() and cast() are too old.

More recently, the functions pivot_long() and pivot_wide() were introduced. (This functions replace gather() and spread() which replaced melt() and cast())

Take a look at this

Orlando Sabogal
  • 1,470
  • 7
  • 20