Home > Software design >  Formatting Dates in R
Formatting Dates in R

Time:11-01

I am trying to clean a dataset in a way where I have a column of dates with the respective months and annual snowfall for the respective month.

I am struggling with doing so, and am asking for some help.

Here is how my data looks currently

Instead of it being columns for each month, I would like it to be where I have one column with each year/month, including a column with the respective annual snowfall.

   SEASON    JUL   AUG   SEP   OCT   NOV   DEC   JAN   FEB   MAR   APR   MAY   JUN TOTAL
   <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 1869-70     0     0     0   0     0     5.3   1.1   9.3   9.6   2.5     0     0  27.8
 2 1870-71     0     0     0   0     0     3    15.9  12.1   0.1   2       0     0  33.1
 3 1871-72     0     0     0   0     0.3   3.9   1.8   3     5.1   0       0     0  14.1
 4 1872-73     0     0     0   0     3.5  27    10.6  18.8   0.4   0       0     0  60.3
 5 1873-74     0     0     0   0     2     9.3   6.6  19     0     0       0     0  36.9
 6 1874-75     0     0     0   0     0    10    14.5   4.5  15.3  13.5     0     0 

CodePudding user response:

Try this: Let's assume your dataframe is called 'df'!

library(dplyr)
df <- df %>% pivot_longer(cols = c(JUL:JUN)) %>% select(SEASON, name, value, TOTAL) %>% rename(MONTH = "name", VALUE = "value")
  •  Tags:  
  • r
  • Related