Problem with mutate on several variables

Hello fellow R users,

I am new to R and I have been facing a problem with using mutate(). I have this dataset with the postal code of French departements and I want to link them to their region using the mutate() tool.

A sample of the dataset :

I tried this code which work for one region but if I use it again with another region it erase the previous region.

Frequentation_musees <- mutate(Frequentation_musees, region = case_when(Frequentation_musees$no_departement %in% c("1", "3", "7", "15", "26", "38", "42", "43", "63", "69", "73", "74") ~ "Auvergne-RhĂ´ne-Alpes"))

So I tried to do the 12 regions in one big chunk of code but I faced this error and I feel it is silly to fix but I can't figure the correct answer.

> Frequentation_musees <- mutate(Frequentation_musees, region = case_when(Frequentation_musees$no_departement %in% c("1", "3", "7", "15", "26", "38", "42", "43", "63", "69", "73", "74") ~ "Auvergne-RhĂ´ne-Alpes"),
Frequentation_musees$no_departement %in% c("89", "21", "70", "90", "25", "39", "71", "58") ~ "Bourgogne-Franche-Comté"),
 Frequentation_musees$no_departement %in% c("8", "55", "54", "57", "67", "68", "88"; "52", "10", "51") ~ "Grand Est"),
Frequentation_musees$no_departement %in% c("62", "59", "2", "80", "60") ~ "Hauts-de-France"), 
Frequentation_musees$no_departement %in% c("75", "92", "93", "94", "95", "77", "91", "78") ~ "Ile-De-France"), Frequentation_musees$no_departement %in% c("50", "14", "61", "27", "76") ~ "Normandie"),
Frequentation_musees$no_departement %in% c("53", "72", "44", "49", "85") ~ "Pays De La Loire"), Frequentation_musees$no_departement %in% c("29", "22", "35", "56") ~ "Bretagne"), Frequentation_musees$no_departement %in% c("62", "59", "2", "80", "28", "45", "41", "18", "37", "36") ~ "Centre-Val De Loire"), 
Frequentation_musees$no_departement %in% c("79", "86", "87", "23", "19", "24", "16", "17", "33", "47", "40", "64") ~ "Nouvelle Aquitaine"), 
Frequentation_musees$no_departement %in% c("46", "12", "48", "30", "34", "81", "82", "32", "31", "65", "9", "11", "66") ~ "Occitanie"), 
Frequentation_musees$no_departement %in% c("13", "84", "4", "5", "6", "83") ~ "Provence-Alpes-CĂ´tes d'Azur") )
Error: unexpected ',' in "uentation_musees$no_departement %in% c("1", "3", "7", "15", "26", "38", "42", "43", "63", "69", "73", "74") ~ "Auvergne-RhĂ´ne-Alpes"),Frequentation_musees$no_departement %in% c("89", "21", "7"

Could anyone help me with this ?

Thanks

Hi,

You had a lot of extra closing parentheses. See cleaned-up code below which uses the "%>%" operator.

Frequentation_musees <- Frequentation_musees %>% 
  mutate(
    region = case_when(
      no_departement %in% c("1", "3", "7", "15", "26", "38", "42", "43", "63", "69", "73", "74") ~ "Auvergne-RhĂ´ne-Alpes",
      no_departement %in% c("89", "21", "70", "90", "25", "39", "71", "58") ~ "Bourgogne-Franche-Comté",
      no_departement %in% c("8", "55", "54", "57", "67", "68", "88"; "52", "10", "51") ~ "Grand Est",
      no_departement %in% c("62", "59", "2", "80", "60") ~ "Hauts-de-France",
      no_departement %in% c("75", "92", "93", "94", "95", "77", "91", "78") ~ "Ile-De-France",
      no_departement %in% c("50", "14", "61", "27", "76") ~ "Normandie",
      no_departement %in% c("53", "72", "44", "49", "85") ~ "Pays De La Loire",
      no_departement %in% c("29", "22", "35", "56") ~ "Bretagne",
      no_departement %in% c("62", "59", "2", "80", "28", "45", "41", "18", "37", "36") ~ "Centre-Val De Loire"
      no_departement %in% c("79", "86", "87", "23", "19", "24", "16", "17", "33", "47", "40", "64") ~ "Nouvelle Aquitaine",
      no_departement %in% c("46", "12", "48", "30", "34", "81", "82", "32", "31", "65", "9", "11", "66") ~ "Occitanie", 
      no_departement %in% c("13", "84", "4", "5", "6", "83") ~ "Provence-Alpes-CĂ´tes d'Azur"
    )
  )
1 Like

Hello Pomchip,

Thank you for your help. It is greatly appreciated :slight_smile:

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