2
I am trying to group the information contained in each of the Brazilian states into regions.
Initially I checked the distribution of these in each state through the function table.
Dice: https://drive.google.com/file/d/1x7pD2yH-u3EZk5TB_bjKzkqyX3K1H-Tv/view?usp=sharing
table(Dados$Estado)
               ACRE             ALAGOAS 
                574                2184 
              AMAPÁ            AMAZONAS 
                728                2624 
              BAHIA               CEARÁ 
               9818                5694 
   DISTRITO FEDERAL      ESPÍRITO SANTO 
              17497                4289 
              GOIÁS            MARANHÃO 
               5896                2705 
        MATO GROSSO  MATO GROSSO DO SUL 
               2984                2683 
       MINAS GERAIS                PARÁ 
              20858                4714 
            PARAÍBA              PARANÁ 
               3428               10608 
         PERNAMBUCO               PIAUÍ 
               6599                1908 
     RIO DE JANEIRO RIO GRANDE DO NORTE 
              26787                2858 
  RIO GRANDE DO SUL            RONDÔNIA 
              11739                1665 
            RORAIMA      SANTA CATARINA 
                514                7246 
          SÃO PAULO             SERGIPE 
              51701                1693 
          TOCANTINS 
               1209 
To accomplish what I intend, I initially tried using chains of ifelse as follows below, however, see that while performing the function again table of the created variable called regions, which contains the information of each state grouped by regions, this does not present the values correctly, that is, the count should be higher since I am grouping each state in its respective region. I also tried through the function case_when package dplyr, however, the problem persists.
Dados$Regioes <- ifelse(Dados$Estado == c("SÃO PAULO","MINAS GERAIS", "ESPÍRITO SANTO", "RIO DE JANEIRO"), "Sudeste",
ifelse(Dados$Estado == c("PARANÁ","SANTA CATARINA", "RIO GRANDE DO SUL"), "Sul",
ifelse(Dados$Estado == c("MATO GROSSO DO SUL", "MATO GROSSO", "GOIÁS","DISTRITO FEDERAL"), "Centro-Oeste",
ifelse(Dados$Estado == c("AMAZONAS", "ACRE", "RONDÔNIA","RORAIMA","PARÁ","TOCANTINS","AMAPÁ"), "Norte",
ifelse(Dados$Estado == c("BAHIA", "SERGIPE", "ALAGOAS","PERNAMBUCO","PARAÍBA","RIO GRANDE DO NORTE","CEARÁ","PIAUÍ","MARANHÃO"), "Nordeste",NA)))))
table(Dados$Regioes)
Centro-Oeste     Nordeste        Norte 
        7338         4052         1707 
     Sudeste          Sul 
       25786         9892
library(dplyr)
Dados$Regioes <- case_when(
Dados$Estado == c("SÃO PAULO","MINAS GERAIS", "ESPÍRITO SANTO", "RIO DE JANEIRO")~ "Sudeste",
Dados$Estado == c("PARANÁ","SANTA CATARINA", "RIO GRANDE DO SUL")~"Sul",
Dados$Estado == c("MATO GROSSO DO SUL", "MATO GROSSO", "GOIÁS","DISTRITO FEDERAL")~"Centro-Oeste",
Dados$Estado == c("AMAZONAS", "ACRE", "RONDÔNIA","RORAIMA","PARÁ","TOCANTINS","AMAPÁ")~ "Norte",
Dados$Estado == c("BAHIA", "SERGIPE", "ALAGOAS","PERNAMBUCO","PARAÍBA","RIO GRANDE DO NORTE","CEARÁ","PIAUÍ","MARANHÃO")~"Nordeste")
table(Dados$Regioes)
Centro-Oeste     Nordeste        Norte 
        7338         4052         1707 
     Sudeste          Sul 
       25786         9892