Indicator in R with more than one condition with duplicate values

1

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 marks only those municipalities that only have "cooperative" and "bank" and do not have "multiple". Resulting on the following basis:

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

I asked a similar question but only with a conditional, and the solution found was with the average pooled indicator:

Indicator in the R variables with duplicate values

    
asked by anonymous 09.01.2018 / 17:17

2 answers

2

If the problem description is correct and the expected result example is not, the following code resolves the issue.

i1 <- grepl("Cooperativa|Banco", dados$IF, ignore.case = TRUE)
i2 <- !grepl("Múltiplo", dados$IF, ignore.case = TRUE)
dados$Indicador <- ave(i1 & i2, dados$Município, FUN = all) + 0L

dados
#   Município          IF Indicador
#1     RIOBOM Cooperativa         1
#2     RIOBOM Cooperativa         1
#3     ABADIA    Múltiplo         0
#4     ABADIA    Múltiplo         0
#5     ABADIA Cooperativa         0
#6     ABADIA       Banco         0
#7   DOURADOS       Banco         0
#8   DOURADOS    Múltiplo         0
#9   DOURADOS       Banco         0
#10  DOURADOS Cooperativa         0
#11  DOURADOS    Múltiplo         0
    
11.01.2018 / 13:31
2

As Rui said, your source database is different from the database with the expected result. Also, I had a different understanding because I think the municipality of RIOBOM would have the indicator 0 because it only has Cooperativa . Here is the code that answers this problem:

df2 <- structure(list(Município = structure(c(3L, 3L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L), .Label = c("ABADIA", "DOURADOS", "RIOBOM"), class = "factor"), 
IF = structure(c(2L, 2L, 3L, 3L, 2L, 1L, 1L, 1L, 2L), .Label = c("Banco", 
"Cooperativa", "Múltiplo"), class = "factor")), .Names = c("Município", 
"IF"), class = "data.frame", row.names = c(NA, -9L))

library(dplyr)

df2 %>% 
  group_by(Município) %>% 
  mutate(Indicador = ifelse( (any(IF == "Cooperativa") & any(IF == "Banco") & !any(IF == "Múltiplo")), 1, 0))

Answer based on this doubt of the OS.

    
12.01.2018 / 13:06