Considere la matriz:

badcombos
      [,1]      [,2] 
[1,] "Red"    "Yellow"
[2,] "Green"  "Yellow"
[3,] "Orange" "Yellow"
[4,] "Blue"   "Green"
[5,] "Blue"   "Purple"

Entonces, la lista:

allcombos
[[1]]
[1] "Red" "Green" "Orange" "Pink" "Purple"
[[2]]
[1] "Red" "Red" "Brown" "Purple" "Pink"
[[3]]
[1] "Yellow" "Red" "Brown" "Blue" "Purple"
[[4]]
[1] "Yellow" "Green" "Blue" "Purple" "Gold"
....
[[k]] "Red" "Blue" "Orange" "Brown" "Pink

¿Cómo "reduciría" la lista en función de las condiciones que si alguna de las combinaciones de colores en la matriz de badcombos aparece en la lista (es decir, sublistas [[3]] y [[4]] en el ejemplo anterior) entonces lo harían? ser eliminado de la lista.

Editar - código completo:

my_list = list()
my_list[[1]] = c("Red","Green","Orange","Pink","Purple")
my_list[[2]] = c("Red","Red","Brown","Purple","Pink")
my_list[[3]] = c("Yellow","Red","Brown","Blue","Purple")
my_list[[4]] = c("Yellow","Green","Blue","Purple","Gold")
my_list[[5]] = c("Red","Blue","Orange","Brown","Pink")

my_matrix = t(matrix(c("Red","Yellow","Green","Yellow","Orange","Yellow","Blue","Green","Blue","Purple"),2,5))
2
Aesler 22 mar. 2017 a las 01:22

2 respuestas

La mejor respuesta

Otra forma de hacer eso:

match_first_col  <- lapply(my_list,match,my_matrix[,1])
match_second_col <- lapply(my_list,match,my_matrix[,2])

#Elements of the list that match both columns:
is.na(mapply(intersect, match_first_col,match_second_col))
[1]  TRUE  TRUE FALSE FALSE  TRUE

#Keep only allcombos with no match in first and second column
my_list[is.na(mapply(intersect, match_first_col,match_second_col))]

[[1]]
[1] "Red"    "Green"  "Orange" "Pink"   "Purple"

[[2]]
[1] "Red"    "Red"    "Brown"  "Purple" "Pink"  

[[3]]
[1] "Red"    "Blue"   "Orange" "Brown"  "Pink" 
2
Pierre Lapointe 21 mar. 2017 a las 23:15

Aquí hay un intento de usar %in% para verificar qué valores de la lista 'incorrecta' están presentes:

sel <- mapply(
  function(a,b) all(rowSums(matrix(b %in% a,ncol=2))<2),
  my_list, list(my_matrix)
)
#[1]  TRUE  TRUE FALSE FALSE  TRUE

Luego, para seleccionar los que desea:

my_list[sel]
1
thelatemail 22 mar. 2017 a las 01:07