Home > Blockchain >  Remove list elements that are not present in another list based on element names
Remove list elements that are not present in another list based on element names

Time:04-18

I have two list that I am working with int1 and int2. Both list have similar names for the list elements. I would like to remove specific components in one list, in this case int2 that are not present in another list int1. Is there a good way to do this in base R? I would like my results to look like the expected_int2.

library(lubridate)
library(tidyverse)
library(purrr)

date <- rep_len(seq(dmy("01-01-2011"), dmy("31-07-2011"), by = "days"), 200)
ID <- rep(c("A","B", "C"), 200)
df <- data.frame(date = date,
                 x = runif(length(date), min = 60000, max = 80000),
                 y = runif(length(date), min = 800000, max = 900000),
                 ID)

df$Month <- month(df$date)

# Create first list
int1 <- df %>%
  # arrange(ID) %>%   # skipped for readability of result
  mutate(new = floor_date(date, '10 day')) %>%
  mutate(new = if_else(day(new) == 31, new - days(10), new)) %>% 
  group_by(ID, new) %>%
  filter(Month == "1") %>% 
  group_split()
# Assign names to int1
names(int1) <- sapply(int1, function(x) paste(x$ID[1],
                                              x$new[1], sep = "_"))
#Remove list elements for the example
int1 <- int1[-c(6, 8, 9)]

# Create second list
int2 <- df %>%
  # arrange(ID) %>%   # skipped for readability of result
  mutate(new = floor_date(date, '10 day')) %>%
  mutate(new = if_else(day(new) == 31, new - days(10), new)) %>% 
  group_by(ID, new) %>%
  filter(Month == "2") %>% 
  group_split()
# Assign names to int2
names(int2) <- sapply(int2, function(x) paste(x$ID[1],
                                              x$new[1], sep = "_"))

# Expected results
expected_int2 <- list(int2[[1]], int2[[2]], int2[[3]], int2[[4]], int2[[5]], int2[[6]])
names(expected_int2) <- sapply(int1, function(x) paste(x$ID[1],
                                              x$new[1], sep = "_"))

CodePudding user response:

We can remove the month part from the names, to check if they are similar to subset

i1 <- sub("(.*)-\\d -(.*)", "\\1-\\2", names(int2)) %in% 
        sub("(.*)-\\d -(.*)", "\\1-\\2", names(int1))
out <- int2[i1]
names(out) <- names(int1)
  • Related