Indicator in R conditioned to variables with duplicate values

3

Suppose there is a base with two variables as follows:

Município   IF
RIOBOM  Cooperativa
RIOBOM  Cooperativa
ABADIA  Múltiplo
ABADIA  Múltiplo
ABADIA  Cooperativa
ABADIA  Banco
DOURADOS    Banco
DOURADOS    Múltiplo
DOURADOS    Banco
DOURADOS    Cooperativa
DOURADOS    Múltiplo

How to create an indicator that only indicates those municipalities that only have "cooperative", independent of having "cooperative" and "bank" or "multiple", it is necessary that only has "cooperative" in the IF variable. Resulting on the following basis:

Município   IF  Indicador
RIOBOM  Cooperativa 1
RIOBOM  Cooperativa 1    
ABADIA  Múltiplo    0
ABADIA  Múltiplo    0
ABADIA  Cooperativa 0
ABADIA  Banco   0
DOURADOS    Banco   0
DOURADOS    Múltiplo    0
DOURADOS    Banco   0
DOURADOS    Cooperativa 0
DOURADOS    Múltiplo    0

I believe the dplyr package works in such a case.

    
asked by anonymous 17.11.2017 / 14:44

2 answers

2

I do not know if there is a more direct way, but the code below solves your problem.

library(dplyr)
df %>% 
  group_by(Município) %>% 
  mutate(Indicador = ifelse(IF == "Cooperativa", 1, 2)) %>% 
  mutate(Indicador = ifelse(mean(Indicador) != 1, 0, 1))
    
17.11.2017 / 15:15
3

Now that it's already answered and accepted, it may not matter, but here it goes. Only with R base, we can use ave .

df$Indicador <- as.integer(ave(df$IF, df$Município, FUN = function(x)
                    all(tolower(x) == "cooperativa")) == "TRUE")
    
17.11.2017 / 20:58