How to change the months in airquality from numbers to names using a function and a if/else statements

This what I have so far
a<-c(airquality[["Month"]])
function(a){
b<-c(if(a==5){
print("May")
} else {
print("June")
})
return(a)
}
a

Hi, I assume this is the built in airquality dataset. There might be a better way, but this will work.


library(tidyverse)

names <- tibble(Month = 1:12,
                MonthName = month.name) # built in dataset

df <- airquality %>% 
  left_join(names, by = "Month")

Actually, I read your heading and you need to use if else statements. That seems like a pain.

library(dplyr)
data("airquality")
unique(airquality$Month)
#> [1] 5 6 7 8 9

the_months <- vector(length = dim(airquality)[1])
for(i in seq_along(airquality$Month)) {
  if(airquality$Month[i] == 5) the_months[i] = "May"
  if(airquality$Month[i] == 6) the_months[i] = "June"
  if(airquality$Month[i] == 7) the_months[i] = "July"
  if(airquality$Month[i] == 8) the_months[i] = "August"
  if(airquality$Month[i] == 9) the_months[i] = "September"
  else "not a month in range"
}
the_months
#>   [1] "May"       "May"       "May"       "May"       "May"       "May"      
#>   [7] "May"       "May"       "May"       "May"       "May"       "May"      
#>  [13] "May"       "May"       "May"       "May"       "May"       "May"      
#>  [19] "May"       "May"       "May"       "May"       "May"       "May"      
#>  [25] "May"       "May"       "May"       "May"       "May"       "May"      
#>  [31] "May"       "June"      "June"      "June"      "June"      "June"     
#>  [37] "June"      "June"      "June"      "June"      "June"      "June"     
#>  [43] "June"      "June"      "June"      "June"      "June"      "June"     
#>  [49] "June"      "June"      "June"      "June"      "June"      "June"     
#>  [55] "June"      "June"      "June"      "June"      "June"      "June"     
#>  [61] "June"      "July"      "July"      "July"      "July"      "July"     
#>  [67] "July"      "July"      "July"      "July"      "July"      "July"     
#>  [73] "July"      "July"      "July"      "July"      "July"      "July"     
#>  [79] "July"      "July"      "July"      "July"      "July"      "July"     
#>  [85] "July"      "July"      "July"      "July"      "July"      "July"     
#>  [91] "July"      "July"      "August"    "August"    "August"    "August"   
#>  [97] "August"    "August"    "August"    "August"    "August"    "August"   
#> [103] "August"    "August"    "August"    "August"    "August"    "August"   
#> [109] "August"    "August"    "August"    "August"    "August"    "August"   
#> [115] "August"    "August"    "August"    "August"    "August"    "August"   
#> [121] "August"    "August"    "August"    "September" "September" "September"
#> [127] "September" "September" "September" "September" "September" "September"
#> [133] "September" "September" "September" "September" "September" "September"
#> [139] "September" "September" "September" "September" "September" "September"
#> [145] "September" "September" "September" "September" "September" "September"
#> [151] "September" "September" "September"

# with dplyr

airquality %>% dplyr::mutate(Month = case_when(
  Month == 5 ~ "May",
  Month == 6 ~ "June",
  Month == 7 ~ "July",
  Month == 8 ~ "August",
  Month == 9 ~ "September"
  )
)
#>     Ozone Solar.R Wind Temp     Month Day
#> 1      41     190  7.4   67       May   1
#> 2      36     118  8.0   72       May   2
#> 3      12     149 12.6   74       May   3
#> 4      18     313 11.5   62       May   4
#> 5      NA      NA 14.3   56       May   5
#> 6      28      NA 14.9   66       May   6
#> 7      23     299  8.6   65       May   7
#> 8      19      99 13.8   59       May   8
#> 9       8      19 20.1   61       May   9
#> 10     NA     194  8.6   69       May  10
#> 11      7      NA  6.9   74       May  11
#> 12     16     256  9.7   69       May  12
#> 13     11     290  9.2   66       May  13
#> 14     14     274 10.9   68       May  14
#> 15     18      65 13.2   58       May  15
#> 16     14     334 11.5   64       May  16
#> 17     34     307 12.0   66       May  17
#> 18      6      78 18.4   57       May  18
#> 19     30     322 11.5   68       May  19
#> 20     11      44  9.7   62       May  20
#> 21      1       8  9.7   59       May  21
#> 22     11     320 16.6   73       May  22
#> 23      4      25  9.7   61       May  23
#> 24     32      92 12.0   61       May  24
#> 25     NA      66 16.6   57       May  25
#> 26     NA     266 14.9   58       May  26
#> 27     NA      NA  8.0   57       May  27
#> 28     23      13 12.0   67       May  28
#> 29     45     252 14.9   81       May  29
#> 30    115     223  5.7   79       May  30
#> 31     37     279  7.4   76       May  31
#> 32     NA     286  8.6   78      June   1
#> 33     NA     287  9.7   74      June   2
#> 34     NA     242 16.1   67      June   3
#> 35     NA     186  9.2   84      June   4
#> 36     NA     220  8.6   85      June   5
#> 37     NA     264 14.3   79      June   6
#> 38     29     127  9.7   82      June   7
#> 39     NA     273  6.9   87      June   8
#> 40     71     291 13.8   90      June   9
#> 41     39     323 11.5   87      June  10
#> 42     NA     259 10.9   93      June  11
#> 43     NA     250  9.2   92      June  12
#> 44     23     148  8.0   82      June  13
#> 45     NA     332 13.8   80      June  14
#> 46     NA     322 11.5   79      June  15
#> 47     21     191 14.9   77      June  16
#> 48     37     284 20.7   72      June  17
#> 49     20      37  9.2   65      June  18
#> 50     12     120 11.5   73      June  19
#> 51     13     137 10.3   76      June  20
#> 52     NA     150  6.3   77      June  21
#> 53     NA      59  1.7   76      June  22
#> 54     NA      91  4.6   76      June  23
#> 55     NA     250  6.3   76      June  24
#> 56     NA     135  8.0   75      June  25
#> 57     NA     127  8.0   78      June  26
#> 58     NA      47 10.3   73      June  27
#> 59     NA      98 11.5   80      June  28
#> 60     NA      31 14.9   77      June  29
#> 61     NA     138  8.0   83      June  30
#> 62    135     269  4.1   84      July   1
#> 63     49     248  9.2   85      July   2
#> 64     32     236  9.2   81      July   3
#> 65     NA     101 10.9   84      July   4
#> 66     64     175  4.6   83      July   5
#> 67     40     314 10.9   83      July   6
#> 68     77     276  5.1   88      July   7
#> 69     97     267  6.3   92      July   8
#> 70     97     272  5.7   92      July   9
#> 71     85     175  7.4   89      July  10
#> 72     NA     139  8.6   82      July  11
#> 73     10     264 14.3   73      July  12
#> 74     27     175 14.9   81      July  13
#> 75     NA     291 14.9   91      July  14
#> 76      7      48 14.3   80      July  15
#> 77     48     260  6.9   81      July  16
#> 78     35     274 10.3   82      July  17
#> 79     61     285  6.3   84      July  18
#> 80     79     187  5.1   87      July  19
#> 81     63     220 11.5   85      July  20
#> 82     16       7  6.9   74      July  21
#> 83     NA     258  9.7   81      July  22
#> 84     NA     295 11.5   82      July  23
#> 85     80     294  8.6   86      July  24
#> 86    108     223  8.0   85      July  25
#> 87     20      81  8.6   82      July  26
#> 88     52      82 12.0   86      July  27
#> 89     82     213  7.4   88      July  28
#> 90     50     275  7.4   86      July  29
#> 91     64     253  7.4   83      July  30
#> 92     59     254  9.2   81      July  31
#> 93     39      83  6.9   81    August   1
#> 94      9      24 13.8   81    August   2
#> 95     16      77  7.4   82    August   3
#> 96     78      NA  6.9   86    August   4
#> 97     35      NA  7.4   85    August   5
#> 98     66      NA  4.6   87    August   6
#> 99    122     255  4.0   89    August   7
#> 100    89     229 10.3   90    August   8
#> 101   110     207  8.0   90    August   9
#> 102    NA     222  8.6   92    August  10
#> 103    NA     137 11.5   86    August  11
#> 104    44     192 11.5   86    August  12
#> 105    28     273 11.5   82    August  13
#> 106    65     157  9.7   80    August  14
#> 107    NA      64 11.5   79    August  15
#> 108    22      71 10.3   77    August  16
#> 109    59      51  6.3   79    August  17
#> 110    23     115  7.4   76    August  18
#> 111    31     244 10.9   78    August  19
#> 112    44     190 10.3   78    August  20
#> 113    21     259 15.5   77    August  21
#> 114     9      36 14.3   72    August  22
#> 115    NA     255 12.6   75    August  23
#> 116    45     212  9.7   79    August  24
#> 117   168     238  3.4   81    August  25
#> 118    73     215  8.0   86    August  26
#> 119    NA     153  5.7   88    August  27
#> 120    76     203  9.7   97    August  28
#> 121   118     225  2.3   94    August  29
#> 122    84     237  6.3   96    August  30
#> 123    85     188  6.3   94    August  31
#> 124    96     167  6.9   91 September   1
#> 125    78     197  5.1   92 September   2
#> 126    73     183  2.8   93 September   3
#> 127    91     189  4.6   93 September   4
#> 128    47      95  7.4   87 September   5
#> 129    32      92 15.5   84 September   6
#> 130    20     252 10.9   80 September   7
#> 131    23     220 10.3   78 September   8
#> 132    21     230 10.9   75 September   9
#> 133    24     259  9.7   73 September  10
#> 134    44     236 14.9   81 September  11
#> 135    21     259 15.5   76 September  12
#> 136    28     238  6.3   77 September  13
#> 137     9      24 10.9   71 September  14
#> 138    13     112 11.5   71 September  15
#> 139    46     237  6.9   78 September  16
#> 140    18     224 13.8   67 September  17
#> 141    13      27 10.3   76 September  18
#> 142    24     238 10.3   68 September  19
#> 143    16     201  8.0   82 September  20
#> 144    13     238 12.6   64 September  21
#> 145    23      14  9.2   71 September  22
#> 146    36     139 10.3   81 September  23
#> 147     7      49 10.3   69 September  24
#> 148    14      20 16.6   63 September  25
#> 149    30     193  6.9   70 September  26
#> 150    NA     145 13.2   77 September  27
#> 151    14     191 14.3   75 September  28
#> 152    18     131  8.0   76 September  29
#> 153    20     223 11.5   68 September  30

# Alternatively

convert_month <- function(x) month.name[x]

airquality$Month <- convert_month(airquality$Month)
1 Like

I believe this is actually a 'one-liner' in R

airquality$Month <-month.name[airquality$Month]

This topic was automatically closed 42 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.