Converting numerical data to categories based on ranges of values in a new column

Hi Everyone,

I am pretty new to using R and I having a hard time taking some aspect data which is in degrees and creating a new column with the corresponding cardinal direction. I am looking to figure out how to tell the program to use a range of degree values I specify for each cardinal direction (N, NE, NW, W, E, SW SE, S) and then to populate a new column with the cardinal direction category and then also to write "Flat" when the aspect is -1. Thanks so much for any advice.

Does the cut function get you what you need?

DF <- data.frame(Deg=seq(0,350,15))
DF$Dir <- cut(DF$Deg,breaks = c(0,seq(22.5,337.5,45),360),
              labels =c("N","NE","E","SE","S","SW","W","NW","N"),
              include.lowest = TRUE)
DF
#>    Deg Dir
#> 1    0   N
#> 2   15   N
#> 3   30  NE
#> 4   45  NE
#> 5   60  NE
#> 6   75   E
#> 7   90   E
#> 8  105   E
#> 9  120  SE
#> 10 135  SE
#> 11 150  SE
#> 12 165   S
#> 13 180   S
#> 14 195   S
#> 15 210  SW
#> 16 225  SW
#> 17 240  SW
#> 18 255   W
#> 19 270   W
#> 20 285   W
#> 21 300  NW
#> 22 315  NW
#> 23 330  NW
#> 24 345   N

Created on 2022-10-01 with reprex v2.0.2

This worked great! Thank you

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.