Transformation of qualitative variables

Hi everyone,
I need help with a simple operation.
I have the following tibble:

tibble(months = as.character(c("April", "May", "October", "November", "December", "August", "May", "January", "February", "March", "July", "June")))

I would like to add a variable to this tibble called "season" that contains the following observation:
-"Winter" when months is = to January, February or March
-"Spring" when month is = to April, March, June
-"Summer" when month is = to July, August, September
-"Fall" when month is = to October, November, December

Thanks for the help


Something like this?


df_tibble <- tibble(months = as.character(c("April", "May", "October", "November", "December", "August", "May", "January", "February", "March", "July", "June")))

df_tibble <- df_tibble %>% mutate(season = ifelse(months == "January" | months == "February" | months == "March", "Winter",
                                     ifelse(months == "April" | months == "May" | months == "June", "Spring",
                                            ifelse(months == "July" | months == "August" | months == "September", "Summer",   

#> # A tibble: 12 x 2
#>    months   season
#>    <chr>    <chr> 
#>  1 April    Spring
#>  2 May      Spring
#>  3 October  Fall  
#>  4 November Fall  
#>  5 December Fall  
#>  6 August   Summer
#>  7 May      Spring
#>  8 January  Winter
#>  9 February Winter
#> 10 March    Winter
#> 11 July     Summer
#> 12 June     Spring

Created on 2020-10-19 by the reprex package (v0.3.0)

1 Like

@ES_StatR if this is the solution you wanted feel free to mark it as such :slight_smile:

1 Like

Thanks for your help,
I was trying the code and it works perfectly with my data!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.