Convert wind direction (degrees) into factors in a data.frame

You can also calculate the breaks with math :nerd_face:

library(tidyverse)

rose_breaks <- c(0, 360/32, (1/32 + (1:15 / 16)) * 360, 360)

rose_labs <- c(
  "North", "North-Northeast", "Northeast", "East-Northeast",
  "East", "East-Southeast", "Southeast", "South-Southeast",
  "South", "South-Southwest", "Southwest", "West-Southwest",
  "West", "West-Northwest", "Northwest", "North-Northwest",
  "North"
)

wind_dir <- tibble(
  wd = (0:16 / 16) * 360
)

wind_dir %>%
  mutate(
    rose = cut(
      wd,
      breaks = rose_breaks,
      labels = rose_labs,
      right = FALSE,
      include.lowest = TRUE
    )
  )
#> # A tibble: 17 x 2
#>       wd rose           
#>    <dbl> <fct>          
#>  1   0   North          
#>  2  22.5 North-Northeast
#>  3  45   Northeast      
#>  4  67.5 East-Northeast 
#>  5  90   East           
#>  6 112.  East-Southeast 
#>  7 135   Southeast      
#>  8 158.  South-Southeast
#>  9 180   South          
#> 10 202.  South-Southwest
#> 11 225   Southwest      
#> 12 248.  West-Southwest 
#> 13 270   West           
#> 14 292.  West-Northwest 
#> 15 315   Northwest      
#> 16 338.  North-Northwest
#> 17 360   North

Created on 2018-09-20 by the reprex package (v0.2.0).

8 Likes