Check if there are different elements in a list in R

Asked

Viewed 130 times

2

I have a list that each element is one data frame containing various information on a particular pension institution. In the column CLASSE there can only be one type and I need to know who owns more than one type. How to create a column that returns TRUE if the INSTITUIDOR have pensioners of different classes.

Below dput of head of the list:

structure(list(`1002538-1` = structure(list(ID.VINC.INST = c("1002538-1", 
"1002538-1"), DS_PREFIXO = c("FILHO (A) INVÁLIDO (A)", "VIÚVA"
), ID.FUNCIONAL = c(43189938L, 43189920L), NR_MATRICULA = c("0094712B", 
"0094712A"), NOME = c("RAQUEL ELCO CAMPOS", "CELINA ELCO CAMPOS"
), ID_FUNCIONAL_INSTITUIDOR = c(1002538L, 1002538L), VINCULO_INSTITUIDOR = c(1L, 
1L), NUMPENS = c(2L, 1L), NM_INSTITUIDOR = c("JOCELINO VILLELA CAMPOS", 
"JOCELINO VILLELA CAMPOS"), VL_BRUTO = c(1182.13, 1182.13), MES_REF = c("01/08/2017", 
"01/08/2017"), CLASSE = c("CLASSE 1", "CLASSE 1"), Freq = c(2L, 
2L)), .Names = c("ID.VINC.INST", "DS_PREFIXO", "ID.FUNCIONAL", 
"NR_MATRICULA", "NOME", "ID_FUNCIONAL_INSTITUIDOR", "VINCULO_INSTITUIDOR", 
"NUMPENS", "NM_INSTITUIDOR", "VL_BRUTO", "MES_REF", "CLASSE", 
"Freq"), row.names = 5:6, class = "data.frame"), `1003437-1` = structure(list(
    ID.VINC.INST = c("1003437-1", "1003437-1"), DS_PREFIXO = c("VIÚVA", 
    "DEPENDENTE"), ID.FUNCIONAL = c(44227795L, 44235089L), NR_MATRICULA = c("0416908A", 
    "0416908B"), NOME = c("VERGINIA MARIA DE OLIVEIRA ROSA", 
    "EMILLY DE OLIVEIRA ROSA"), ID_FUNCIONAL_INSTITUIDOR = c(1003437L, 
    1003437L), VINCULO_INSTITUIDOR = c(1L, 1L), NUMPENS = 1:2, 
    NM_INSTITUIDOR = c("ANTONIO PLACIDIO LOPES ROSA", "ANTONIO PLACIDIO LOPES ROSA"
    ), VL_BRUTO = c(3047.47, 3047.47), MES_REF = c("01/08/2017", 
    "01/08/2017"), CLASSE = c("CLASSE 1", "OUTRAS"), Freq = c(2L, 
    2L)), .Names = c("ID.VINC.INST", "DS_PREFIXO", "ID.FUNCIONAL", 
"NR_MATRICULA", "NOME", "ID_FUNCIONAL_INSTITUIDOR", "VINCULO_INSTITUIDOR", 
"NUMPENS", "NM_INSTITUIDOR", "VL_BRUTO", "MES_REF", "CLASSE", 
"Freq"), row.names = 10:11, class = "data.frame"), `10087958-1` = structure(list(
    ID.VINC.INST = c("10087958-1", "10087958-1"), DS_PREFIXO = c("VIÚVA", 
    "FILHO (A) MENOR"), ID.FUNCIONAL = c(50740172L, 50740202L
    ), NR_MATRICULA = c("", ""), NOME = c("ALEXANDRA GOMES DE OLIVEIRA MACIEL", 
    "JUAN PEDRO DE OLIVEIRA MACIEL"), ID_FUNCIONAL_INSTITUIDOR = c(10087958L, 
    10087958L), VINCULO_INSTITUIDOR = c(1L, 1L), NUMPENS = 1:2, 
    NM_INSTITUIDOR = c("MARCO ANTONIO DE OLIVEIRA MACIEL", "MARCO ANTONIO DE OLIVEIRA MACIEL"
    ), VL_BRUTO = c(3076.8, 3076.8), MES_REF = c("01/08/2017", 
    "01/08/2017"), CLASSE = c("CLASSE 1", "CLASSE 1"), Freq = c(2L, 
    2L)), .Names = c("ID.VINC.INST", "DS_PREFIXO", "ID.FUNCIONAL", 
"NR_MATRICULA", "NOME", "ID_FUNCIONAL_INSTITUIDOR", "VINCULO_INSTITUIDOR", 
"NUMPENS", "NM_INSTITUIDOR", "VL_BRUTO", "MES_REF", "CLASSE", 
"Freq"), row.names = 19:20, class = "data.frame"), `10114963-1` = structure(list(
    ID.VINC.INST = c("10114963-1", "10114963-1"), DS_PREFIXO = c("FILHO (A) MENOR", 
    "VIÚVA"), ID.FUNCIONAL = c(50853104L, 50853090L), NR_MATRICULA = c("", 
    ""), NOME = c("GUILHERME DE ASSIS MARINHO", "ELISANGELA DE ASSIS SILVA MARINHO"
    ), ID_FUNCIONAL_INSTITUIDOR = c(10114963L, 10114963L), VINCULO_INSTITUIDOR = c(1L, 
    1L), NUMPENS = c(2L, 1L), NM_INSTITUIDOR = c("EDSON BORBA MARINHO", 
    "EDSON BORBA MARINHO"), VL_BRUTO = c(3095.41, 3095.41), MES_REF = c("01/08/2017", 
    "01/08/2017"), CLASSE = c("CLASSE 1", "CLASSE 1"), Freq = c(2L, 
    2L)), .Names = c("ID.VINC.INST", "DS_PREFIXO", "ID.FUNCIONAL", 
"NR_MATRICULA", "NOME", "ID_FUNCIONAL_INSTITUIDOR", "VINCULO_INSTITUIDOR", 
"NUMPENS", "NM_INSTITUIDOR", "VL_BRUTO", "MES_REF", "CLASSE", 
"Freq"), row.names = 27:28, class = "data.frame"), `10179151-1` = structure(list(
    ID.VINC.INST = c("10179151-1", "10179151-1", "10179151-1"
    ), DS_PREFIXO = c("FILHO (A) MENOR", "COMPANHEIRA", "FILHO (A) MENOR"
    ), ID.FUNCIONAL = c(43038824L, 43300464L, 43038816L), NR_MATRICULA = c("0410258B", 
    "0410258C", "0410258A"), NOME = c("ERICA FERREIRA ROGERIO DE SOUZA", 
    "PATRICIA FERREIRA DA SILVA", "ERIC ROGERIO DE SOUZA JUNIOR"
    ), ID_FUNCIONAL_INSTITUIDOR = c(10179151L, 10179151L, 10179151L
    ), VINCULO_INSTITUIDOR = c(1L, 1L, 1L), NUMPENS = c(2L, 3L, 
    1L), NM_INSTITUIDOR = c("ERIC ROGERIO DE SOUZA", "ERIC ROGERIO DE SOUZA", 
    "ERIC ROGERIO DE SOUZA"), VL_BRUTO = c(416.04, 832.09, 416.04
    ), MES_REF = c("01/08/2017", "01/08/2017", "01/08/2017"), 
    CLASSE = c("CLASSE 1", "CLASSE 1", "CLASSE 1"), Freq = c(3L, 
    3L, 3L)), .Names = c("ID.VINC.INST", "DS_PREFIXO", "ID.FUNCIONAL", 
"NR_MATRICULA", "NOME", "ID_FUNCIONAL_INSTITUIDOR", "VINCULO_INSTITUIDOR", 
"NUMPENS", "NM_INSTITUIDOR", "VL_BRUTO", "MES_REF", "CLASSE", 
"Freq"), row.names = 40:42, class = "data.frame"), `1031945-1` = structure(list(
    ID.VINC.INST = c("1031945-1", "1031945-1"), DS_PREFIXO = c("FILHO (A) MENOR", 
    "FILHO (A) MENOR"), ID.FUNCIONAL = c(50840835L, 50840827L
    ), NR_MATRICULA = c("", ""), NOME = c("LUIZ SERGIO RAMOS TORRES", 
    "JULIANA RAMOS TORRES"), ID_FUNCIONAL_INSTITUIDOR = c(1031945L, 
    1031945L), VINCULO_INSTITUIDOR = c(1L, 1L), NUMPENS = c(2L, 
    1L), NM_INSTITUIDOR = c("ETEVALDO CORREA TORRES", "ETEVALDO CORREA TORRES"
    ), VL_BRUTO = c(656.83, 656.83), MES_REF = c("01/08/2017", 
    "01/08/2017"), CLASSE = c("CLASSE 1", "CLASSE 1"), Freq = c(2L, 
    2L)), .Names = c("ID.VINC.INST", "DS_PREFIXO", "ID.FUNCIONAL", 
"NR_MATRICULA", "NOME", "ID_FUNCIONAL_INSTITUIDOR", "VINCULO_INSTITUIDOR", 
"NUMPENS", "NM_INSTITUIDOR", "VL_BRUTO", "MES_REF", "CLASSE", 
"Freq"), row.names = 86:87, class = "data.frame")), .Names = c("1002538-1", 
"1003437-1", "10087958-1", "10114963-1", "10179151-1", "1031945-1"
))
  • I don’t fully understand, either the new column in each data.frame? For example, the first df has two lines, both with the same class, CLASSE 1. Want a new column with both values FALSE?

  • Exactly. My difficulty in this problem is that each data frame may have a different number of lines.

2 answers

4

The following instruction lapply creates a new column called DUPL_CLASSE in each data.frame. If all values in the column CLASSE are the same, the new column is all FALSE, otherwise it’s all TRUE.

nova_lista <- lapply(lista, function(x){
        x$DUPL_CLASSE <- length(unique(x$CLASSE)) > 1
        x
    })

(I called lista to the question list.)

3


It may not be the most elegant shape,

df <- data.frame(LISTA = names(dados), CLASSE = T)
for(i in 1:nrow(df)){
 ifelse(length(unique(dados[[i]]$CLASSE)) == 1, df$CLASSE[i] <- F, df$CLASSE[i] <- T)
}

being dados, the list you provided.

  • I made the following mistake: Error in multpensao.semcotista[[i]]$CLASSE : &#xA; $ operator is invalid for atomic vectors. EDIT: I used the wrong variable here, fixed it and it worked. Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.