I have a dataset like that
Month | Year | Region |
---|---|---|
January | 2019 | NY |
February | 2019 | NY |
March | 2019 | NY |
September | 2019 | NY |
I want to add a column with season time like this:
Month | Year | Region | Season |
---|---|---|---|
January | 2019 | NY | Winter |
February | 2019 | NY | Winter |
March | 2019 | NY | Spring |
September | 2019 | NY | Fall |
How can I do a code in R that automatically add a column where all January, February and December are Winter, all March, April and May are Spring and so on.
Thanks a lot for helping
season <- c(data, Spring = "March", Spring = "April")
CodePudding user response:
We can create a keyvalue dataset and do a join
library(dplyr)
keydat <- tibble(Month = month.name,
Season = rep(c("Winter", "Spring", "Summer", "Fall", "Winter"),
c(2, 3, 3, 3, 1)))
df1 <- left_join(df1, keydat)
-output
df1
Month Year Region Season
1 January 2019 NY Winter
2 February 2019 NY Winter
3 March 2019 NY Spring
4 September 2019 NY Fall
data
df1 <- structure(list(Month = c("January", "February", "March", "September"
), Year = c(2019L, 2019L, 2019L, 2019L), Region = c("NY", "NY",
"NY", "NY")), class = "data.frame", row.names = c(NA, -4L))
CodePudding user response:
In base R you could do:
df1$Season <- c('Winter', 'Spring', 'Summer', 'Fall')[
1 (match(df1$Month, month.name) %/% 3) %% 4]
Which results in:
df1
#> Month Year Region Season
#> 1 January 2019 NY Winter
#> 2 February 2019 NY Winter
#> 3 March 2019 NY Spring
#> 4 September 2019 NY Fall
(Using akrun's reproducible data)