Assign name in column based on values from another column in R

Solution 1:

We can use dplyr. case_when is the way to go here.

library(dplyr)

rdf %>%
    mutate(Region = case_when(Unit %in% 1:13 ~ "East",
                              Unit %in% c(14:25, 27) ~ "Central", 
                              Unit %in% c(26, 28:38, 40:43, 45:46) ~ "West",
                              Unit %in% c(44, 39, 47:60) ~ "BC"))
   Unit  Region
1     1    East
2     2    East
3     3    East
4     4    East
5     5    East
6     6    East
7     7    East
8     8    East
9     9    East
10   10    East
11   11    East
12   12    East
13   13    East
14   14 Central
15   15 Central
16   16 Central
17   17 Central
18   18 Central
19   19 Central
20   20 Central
21   21 Central
22   22 Central
23   23 Central
24   24 Central
25   25 Central
26   26    West
27   27 Central
28   28    West
29   29    West
30   30    West
31   31    West
32   32    West
33   33    West
34   34    West
35   35    West
36   37    West
37   38    West
38   39      BC
39   40    West
40   41    West
41   42    West
42   43    West
43   44      BC
44   45    West
45   46    West
46   47      BC
47   48      BC
48   49      BC
49   50      BC
50   51      BC
51   52      BC
52   53      BC
53   54      BC
54   55      BC
55   56      BC
56   57      BC
57   58      BC
58   59      BC
59   60      BC