Reputation: 63
I have a dataset that has a variable called month, which each month as a character. Is there a way with dplyr to combine some months to create a season variable? I have tried the following but got an error:
data %>%
mutate(season = ifelse(month[1:3], "Winter", ifelse(month[4:6], "Spring",
ifelse(month[7:9], "Summer",
ifelse(month[10:12], "Fall", NA)))))
With error:
Error in mutate_impl(.data, dots) : Column `season` must be length 100798 (the number of rows) or one, not 3
I am new to R so any help is much appreciated!
Upvotes: 4
Views: 7289
Reputation: 28341
The correct syntax should be
data %>% mutate(season = ifelse(month %in% 10:12, "Fall",
ifelse(month %in% 1:3, "Winter",
ifelse(month %in% 4:6, "Spring",
"Summer"))))
Edit: probably a better way to get the job done
temp_data %>%
mutate(
season = case_when(
month %in% 10:12 ~ "Fall",
month %in% 1:3 ~ "Winter",
month %in% 4:6 ~ "Spring",
TRUE ~ "Summer"))
Meteorological Seasons
temp_data %>%
mutate(
season = case_when(
month %in% 9:11 ~ "Fall",
month %in% c(12, 1, 2) ~ "Winter",
month %in% 3:5 ~ "Spring",
TRUE ~ "Summer"))
Upvotes: 6
Reputation: 887048
When there are multiple key/value, we can do a join with a key/val dataset
keyval <- data.frame(month = month.abb,
season = rep(c("Winter", "Spring", "Summer", "Fall"), each = 3),
stringsAsFactors = FALSE)
left_join(data, keyval)
Upvotes: 4
Reputation: 15072
You can also try using dplyr::recode
or functions from forcats
. I think this is the simplest method here:
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
data <- tibble(month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
data %>%
mutate(
season = fct_collapse(
.f = month,
Spring = c("Mar", "Apr", "May"),
Summer = c("Jun", "Jul", "Aug"),
Autumn = c("Sep", "Oct", "Nov"),
Winter = c("Dec", "Jan", "Feb")
)
)
#> # A tibble: 12 x 2
#> month season
#> <chr> <fct>
#> 1 Jan Winter
#> 2 Feb Winter
#> 3 Mar Spring
#> 4 Apr Spring
#> 5 May Spring
#> 6 Jun Summer
#> 7 Jul Summer
#> 8 Aug Summer
#> 9 Sep Autumn
#> 10 Oct Autumn
#> 11 Nov Autumn
#> 12 Dec Winter
Created on 2018-04-06 by the reprex package (v0.2.0).
Upvotes: 1