首页 > 解决方案 > 使用循环查找列表之间的匹配词

问题描述

我有两个数据集。一个是包含 72 个项目的列表,其中每个项目本身就是一个包含 10 个句子的列表。因此,我总共有 720 个句子,每个句子由 10 个列表分隔。

第二组数据是第一个数据集中所有以“ing”结尾的单词的列表。

我想查看每个列表项,如果在所述列表的十个句子中的任何一个中包含“ing”词。

如果是这样,列表中出现了哪些单词,这是该单词第一次出现在整个数据集中(即,第一次出现在所有 720 个句子中)?然后我计划将所有这些信息编译成一个表

这就是我到目前为止所拥有的。我只是想看看它是否会打印出每个 ing 单词所在的列表,然后再转到更复杂的部分。

n <- 1

harvardList[1]
for(word in IngWords){
  if(IngWords==harvardList[n])
  print(harvardList[n])
  n <- n+1
}

当我运行该脚本时,我收到这些错误并输出:

Error: unexpected 'in' in:
"for(word in IngWords){
  if(word in"
 print(harvardList[n])
$`List 1`
$`List 1`[[1]]
[1] "The birch canoe slid on the smooth planks."

etc., 

>   n <- n+1
> }
Error: unexpected '}' in "}"

这是句子列表的迷你版:

$`List 1`[[1]]
[1] "The source of the huge river is the clear spring."

$`List 1`[[2]]
[1] "Help the woman get back to her feet."

$`List 1`[[3]]
[1] "A pot of tea helps to pass the evening."

$`List 2`[[1]]
[1] "The colt reared and threw the tall rider."

$`List 2`[[2]]
[1] "It snowed, rained, and hailed the same morning."

$`List 2`[[3]]
[1] "Read verse out loud for pleasure."

$`List 3`[[1]]
[1] "Take the winding path to reach the lake."

$`List 3`[[2]]
[1] "The meal was cooked before the bell rang."

$`List 3`[[3]]
[1] "What joy there is in living."

这些是 ing 词:

生活 蜿蜒 早晨 傍晚 春天

预期输出:

[List Number] [ing-word]
1             spring, evening
2             morning
3             winding, living

标签: rloops

解决方案


我们可以使用 遍历列表中的每个元素,lapply在空格上拆分每个单词,删除标点符号并找到出现在 中的单词IngWords

stack(lapply(harvardList, function(x) {
   all_words <- gsub("[[:punct:]]", "", unlist(strsplit(unlist(x), " ")))
   toString(all_words[all_words %in% IngWords])
}))[2:1]


#    ind          values
#1 List1 spring, evening
#2 List2         morning
#3 List3 winding, living

数据

harvardList <- list(List1= list("The source of the huge river is the clear spring.",
              "Help the woman get back to her feet.", 
              "A pot of tea helps to pass the evening."), 
 List2 = list("The colt reared and threw the tall rider.", 
              "It snowed, rained, and hailed the same morning.", 
              "Read verse out loud for pleasure."), 
 List3 = list( "Take the winding path to reach the lake.", 
               "The meal was cooked before the bell rang.", 
               "What joy there is in living."))
IngWords <- c("living", "winding", "morning", "evening", "spring")

推荐阅读