简体   繁体   中英

Issues reshaping data.frame from wide to long format

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM