如何根据R中的另一个列表选择列表的值?

khbbv19g  于 2022-12-25  发布在  其他
关注(0)|答案(3)|浏览(113)

我有一个包含值的列表

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

我还有一份名单

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")

我只想从第二个列表中提取以第一个列表中的值开头的列表。

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

我的最终列表不应该包括那些2022年10月的文件,因为它没有包括在第一个列表中。

db2dz4w8

db2dz4w81#

我们可以通过paste将第一个向量(v1)中的值作为单个模式来使用grep,以匹配和子集化来自主向量'v2'的值

grep(paste(v1, collapse = "|"), v2, value = TRUE)
  • 输出
[1] "December 2022/File1.xlsx" "December 2022/File2.xlsx" 
[3] "November 2022/File1.xlsx" "November 2022/File2.xlsx"

或者从第二个向量中删除子字符串后缀,使用%in%创建一个逻辑向量以将"v2"作为子集

v2[trimws(v2, whitespace = "/.*") %in% v1]
  • 输出
[1] "December 2022/File1.xlsx" "December 2022/File2.xlsx"
[3] "November 2022/File1.xlsx" "November 2022/File2.xlsx"
ccgok5k5

ccgok5k52#

我们可以Vectorizegrepl并在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"
knsnq2tg

knsnq2tg3#

另一个选项是将“匿名”Vectorizegrep一起使用

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"

接近于sapplygrep

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"
数据
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")

相关问题