gpt4 book ai didi

r - 使用循环查找列表之间的匹配词

转载 作者:行者123 更新时间:2023-12-04 10:56:25 25 4
gpt4 key购买 nike

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

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

我想查看每个列表项,是否在所述列表的十个句子中的任何一个中包含“ing”字样。

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

这是我到目前为止。在进入更复杂的部分之前,我只是想看看它是否会打印出每个单词所在的列表。

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词:

生活曲折的早晨傍晚 Spring

预期输出:
[List Number] [ing-word]
1 spring, evening
2 morning
3 winding, living

最佳答案

我们可以使用 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")

关于r - 使用循环查找列表之间的匹配词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59149260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com