Agregar un elemento de la lista si está ausente en una comparación de dos listas -- r campo con list campo con for-loop camp Relacionados El problema

Adding a list element if it is absent in a comparison of two lists


1
vote

problema

Español

Tengo datos de la siguiente manera:

  DT1 <- structure(list(Province = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,  2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3), Year = c(2000,  2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000,  2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000, 2001, 2001,  2001, 2002, 2002, 2002), Municipality = c("Something", "Anything",  "Nothing", "Something", "Anything", "Nothing", "Something", "Anything",  "Nothing", "Something", "Anything", "Nothing", "Something", "Anything",  "Nothing", "Something", "Anything", "Nothing", "Something", "Anything",  "Nothing", "Something", "Anything", "Nothing", "Something", "Anything",  "Nothing"), Values = c(0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86,  0.85, 0.99, 0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99,  0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99)), row.names = c(NA,  -27L), class = c("tbl_df", "tbl", "data.frame"))  DT2 <- structure(list(Province = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,  2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3), Year = c(2000,  2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000,  2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000, 2001, 2001,  2001, 2002, 2002, 2002), Municipality = c("Some", "Anything",  "Nothing", "Someth.", "Anything", "Not", "Something", "Anything",  "None", "Some", "Anything", "Nothing", "Someth.", "Anything",  "Not", "Something", "Anything", "None", "Some", "Anything", "Nothing",  "Someth.", "Anything", "Not", "Something", "Anything", "None" ), `Other Values` = c(0.41, 0.42, 0.34, 0.47, 0.0600000000000001,  0.8, 0.14, 0.15, 0.01, 0.41, 0.42, 0.34, 0.47, 0.0600000000000001,  0.8, 0.14, 0.15, 0.01, 0.41, 0.42, 0.34, 0.47, 0.0600000000000001,  0.8, 0.14, 0.15, 0.01)), row.names = c(NA, -27L), class = c("tbl_df",  "tbl", "data.frame")) DT2 <- DT2[-c(5:10),]  DT1_list <- DT1%>%   group_split(Province, Year)  DT2_list <- DT2%>%   group_split(Province, Year)   

Los "subthrames" en DT1_list y - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if ([_delegate respondsToSelector:@selector(ListItemBeginTouched:)]) { [_delegate ListItemBeginTouched:self]; } 0 se dividen mediante - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if ([_delegate respondsToSelector:@selector(ListItemBeginTouched:)]) { [_delegate ListItemBeginTouched:self]; } 111111 , pero no coinciden. Para la operación que quiero hacer, los necesito para que coincidan. Estaba pensando en usar un - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if ([_delegate respondsToSelector:@selector(ListItemBeginTouched:)]) { [_delegate ListItemBeginTouched:self]; } 2 para insertar un subtrama, siempre que el - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if ([_delegate respondsToSelector:@selector(ListItemBeginTouched:)]) { [_delegate ListItemBeginTouched:self]; } 3 del SubFrame de - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if ([_delegate respondsToSelector:@selector(ListItemBeginTouched:)]) { [_delegate ListItemBeginTouched:self]; } 4 no coincide con el subgramante de - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if ([_delegate respondsToSelector:@selector(ListItemBeginTouched:)]) { [_delegate ListItemBeginTouched:self]; } 5 , pero no puedo averiguar cómo hacerlo.

  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if ([_delegate respondsToSelector:@selector(ListItemBeginTouched:)]) {     [_delegate ListItemBeginTouched:self]; } 6  
Original en ingles

I have example data as follows:

DT1 <- structure(list(Province = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,  2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3), Year = c(2000,  2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000,  2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000, 2001, 2001,  2001, 2002, 2002, 2002), Municipality = c("Something", "Anything",  "Nothing", "Something", "Anything", "Nothing", "Something", "Anything",  "Nothing", "Something", "Anything", "Nothing", "Something", "Anything",  "Nothing", "Something", "Anything", "Nothing", "Something", "Anything",  "Nothing", "Something", "Anything", "Nothing", "Something", "Anything",  "Nothing"), Values = c(0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86,  0.85, 0.99, 0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99,  0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99)), row.names = c(NA,  -27L), class = c("tbl_df", "tbl", "data.frame"))  DT2 <- structure(list(Province = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,  2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3), Year = c(2000,  2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000,  2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000, 2001, 2001,  2001, 2002, 2002, 2002), Municipality = c("Some", "Anything",  "Nothing", "Someth.", "Anything", "Not", "Something", "Anything",  "None", "Some", "Anything", "Nothing", "Someth.", "Anything",  "Not", "Something", "Anything", "None", "Some", "Anything", "Nothing",  "Someth.", "Anything", "Not", "Something", "Anything", "None" ), `Other Values` = c(0.41, 0.42, 0.34, 0.47, 0.0600000000000001,  0.8, 0.14, 0.15, 0.01, 0.41, 0.42, 0.34, 0.47, 0.0600000000000001,  0.8, 0.14, 0.15, 0.01, 0.41, 0.42, 0.34, 0.47, 0.0600000000000001,  0.8, 0.14, 0.15, 0.01)), row.names = c(NA, -27L), class = c("tbl_df",  "tbl", "data.frame")) DT2 <- DT2[-c(5:10),]  DT1_list <- DT1%>%   group_split(Province, Year)  DT2_list <- DT2%>%   group_split(Province, Year) 

The "subframes" in DT1_list and DT2_list are split out by Province, Year, but they do no match. For the operation I want to do, I need them to match. I was thinking about using a for-loop to insert a subframe, whenever the Province, Year of the subframe of DT1_list does not match the subframe of DT2_list , but I cannot figure out how to do it.

for (i in 1:9) {   if (DT1_list[[i]][[Province, Year]] != DT2_list[[i]][[Province, Year]]) {     DT2_list[[i]] <- "Put a list with the province and year in between"   } }          
        
     
     

Lista de respuestas

1
 
vote
vote
La mejor respuesta
 

Modificar DF ​​más pequeño como este

  library(tidyverse)  DT2_modified <- DT1 %>% select(Province, Year) %>% group_by(Province, Year) %>%    slice_head() %>%   left_join(DT2, by = c("Province", "Year"))  #Now extract your lists DT2_list_modified <- DT2_modified %>%   group_split(Province, Year)   #Check the code/result > DT2_list_modified <list_of<   tbl_df<     Province    : double     Year        : double     Municipality: character     Other Values: double   > >[9]> [[1]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        1  2000 Some                   0.41 2        1  2000 Anything               0.42 3        1  2000 Nothing                0.34  [[2]] # A tibble: 1 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        1  2001 Someth.                0.47  [[3]] # A tibble: 1 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        1  2002 NA                       NA  [[4]] # A tibble: 2 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        2  2000 Anything               0.42 2        2  2000 Nothing                0.34  [[5]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        2  2001 Someth.                0.47 2        2  2001 Anything               0.06 3        2  2001 Not                    0.8   [[6]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        2  2002 Something              0.14 2        2  2002 Anything               0.15 3        2  2002 None                   0.01  [[7]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        3  2000 Some                   0.41 2        3  2000 Anything               0.42 3        3  2000 Nothing                0.34  [[8]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        3  2001 Someth.                0.47 2        3  2001 Anything               0.06 3        3  2001 Not                    0.8   [[9]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        3  2002 Something              0.14 2        3  2002 Anything               0.15 3        3  2002 None                   0.01   

Creo que esto servirá al propósito

 

Modify smaller DF like this

library(tidyverse)  DT2_modified <- DT1 %>% select(Province, Year) %>% group_by(Province, Year) %>%    slice_head() %>%   left_join(DT2, by = c("Province", "Year"))  #Now extract your lists DT2_list_modified <- DT2_modified %>%   group_split(Province, Year)   #Check the code/result > DT2_list_modified <list_of<   tbl_df<     Province    : double     Year        : double     Municipality: character     Other Values: double   > >[9]> [[1]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        1  2000 Some                   0.41 2        1  2000 Anything               0.42 3        1  2000 Nothing                0.34  [[2]] # A tibble: 1 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        1  2001 Someth.                0.47  [[3]] # A tibble: 1 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        1  2002 NA                       NA  [[4]] # A tibble: 2 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        2  2000 Anything               0.42 2        2  2000 Nothing                0.34  [[5]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        2  2001 Someth.                0.47 2        2  2001 Anything               0.06 3        2  2001 Not                    0.8   [[6]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        2  2002 Something              0.14 2        2  2002 Anything               0.15 3        2  2002 None                   0.01  [[7]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        3  2000 Some                   0.41 2        3  2000 Anything               0.42 3        3  2000 Nothing                0.34  [[8]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        3  2001 Someth.                0.47 2        3  2001 Anything               0.06 3        3  2001 Not                    0.8   [[9]] # A tibble: 3 x 4   Province  Year Municipality `Other Values`      <dbl> <dbl> <chr>                 <dbl> 1        3  2002 Something              0.14 2        3  2002 Anything               0.15 3        3  2002 None                   0.01 

I think this will serve the purpose

 
 
 
 

Relacionados problema

10  ¿Hay algún valor en el uso del mapa () vs?  ( Is there a value in using map vs for ) 
¿El mapa () itera a través de la lista como "para" lo haría? ¿Hay algún valor en el uso del mapa VS para? Si es así, en este momento mi código se ve así: ...

0  Cómo hacer Diccionario de Árbol con Python  ( How to make tree dictionary with python ) 
Necesito ayuda con un problema de Python que estoy teniendo. Tengo algunos datos de una base de datos MySQL que quiero hacer un diccionario de padres / hijos....

2  Error / manejo de excepciones para el bucle - Python  ( Error exception handling in for loop python ) 
Estoy usando la API de Google Cloud NL para analizar el sentimiento de algunas descripciones. En cuanto a algunas filas, el error InvalidArgument: 400 The la...

0  Nunca terminando para el bucle en Python  ( Never ending for loop in python ) 
Tengo un código que básicamente toma dos imágenes, imagen grande y imagen pequeña. La imagen pequeña se está reduciendo en una imagen de una fila y luego se e...

0  La función de salida es la función con el índice de matriz como entrada  ( Output function is function with matrix index as input ) 
Necesito escribir un procedimiento iterativo que siga sumando el valor de una función en 2 variables a,b a otras funciones con las mismas entradas a,b . Pa...

0  Un pequeño problema que estoy enfrentando en MVC2 con separación de controladores y modelos en una clase diferente  ( A little issue i am facing in mvc2 with separating controllers and models in a d ) 
Si estoy usando un proyecto de biblioteca de clase diferente para mantener mis controladores, ¿cómo crearé los controladores? Anteriormente, lo uso para hacer...

2  R - petize condicional reemplazar  ( R vectorised conditional replace ) 
Hola, estoy intentando manipular una lista de números y me gustaría hacerlo sin un bucle para el bucle rápido en R. El pseudocódigo para la manipulación es: ...

5  SELENIO WEBDRIVER WINDAY SHIPS C # SWITCHTE FAIL  ( Selenium webdriver window handles c sharp switchto failed ) 
Aquí viene 2 Windows saliendo durante las pruebas. Mi código: HelloListView/Resources/layout/111 Quiero cambiar a la ventana que la URL contiene "Main...

0  Anidado para el bucle, buscando archivos  ( Nested for loop searching files ) 
Tengo dos archivos: filetest.txt ============ SSISPACKAGE1.dtsx SSISPACKAGE2.dtsx SSISPACKAGE3.dtsx SSISPACKAGE4.dtsx SSISPACKAGE5.dtsx SSISPACKAGE6.d...

5  Unir listas por valor  ( Join lists by value ) 
Hay una forma eficiente de fusionar dos listas de tuplas en Python, basadas en un valor común. Actualmente, estoy haciendo lo siguiente: name = [ ...




© 2022 respuesta.top Reservados todos los derechos. Centro de preguntas y respuestas reservados todos los derechos