Home > Back-end >  How to select values of a list based on another list in R?
How to select values of a list based on another list in R?

Time:12-23

I have a list that has values

c("December 2022","November 2022")

I have another list that has

c("December 2022/File1.xlsx","December 2022/File2.xlsx","October 2022/File1.xlsx",
"October 2022/File2.xlsx","November 2022/File1.xlsx","November 2022/File2.xlsx")

I want to pull from second list only the ones that start with the values in the first list. My final list should include

c("December 2022/File1.xlsx","December 2022/File2.xlsx","November 
2022/File1.xlsx","November 2022/File2.xlsx")

My final list should not include those October 2022 files because it was not included in the first one.

CodePudding user response:

We can use grep by pasteing the values in the first vector (v1) as a single pattern to match and subset the values from the main vector 'v2'

grep(paste(v1, collapse = "|"), v2, value = TRUE)

-output

[1] "December 2022/File1.xlsx" "December 2022/File2.xlsx" 
[3] "November 2022/File1.xlsx" "November 2022/File2.xlsx"

Or remove the substring suffix from second vector, create a logical vector with %in% to subset the 'v2'

v2[trimws(v2, whitespace = "/.*") %in% v1]

-output

[1] "December 2022/File1.xlsx" "December 2022/File2.xlsx"
[3] "November 2022/File1.xlsx" "November 2022/File2.xlsx"

CodePudding user response:

We could Vectorize grepl and use it in outer.

l2[colSums(outer(l1, l2, Vectorize(grepl))) > 0]
# [1] "December 2022/File1.xlsx" "December 2022/File2.xlsx"
# [3] "November 2022/File1.xlsx" "November 2022/File2.xlsx"

CodePudding user response:

Another option using "anonymous" Vectorize with grep

as.vector(Vectorize(\(x) grep(x, list1, value=T))(val))
[1] "December 2022/File1.xlsx" "December 2022/File2.xlsx"
[3] "November 2022/File1.xlsx" "November 2022/File2.xlsx"

which is close to sapply and grep

as.vector(sapply(val, grep, list1, value=T))
[1] "December 2022/File1.xlsx" "December 2022/File2.xlsx"
[3] "November 2022/File1.xlsx" "November 2022/File2.xlsx"

Data

val <- c("December 2022","November 2022")
list1 <- c("December 2022/File1.xlsx","December 2022/File2.xlsx","October 2022/File1.xlsx",
"October 2022/File2.xlsx","November 2022/File1.xlsx","November 2022/File2.xlsx")
  • Related