【问题标题】:Using loops to find matching words between lists使用循环查找列表之间的匹配词
【发布时间】:2019-12-03 02:11:52
【问题描述】:

我有两个数据集。一个是包含 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

【问题讨论】:

    标签: r loops


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我们可以使用tidyverse 来做到这一点

      library(stringr)
      library(tibble)
      library(purrr)
      enframe(harvardList) %>% 
           unnest(c(value)) %>% 
           unnest(value) %>% 
           group_by(name) %>%
           summarise(value = toString(na.omit(str_extract(value, 
               str_c(IngWords, collapse="|")))))
      # A tibble: 3 x 2
      #  name  value          
      #  <chr> <chr>          
      #1 List1 spring, evening
      #2 List2 morning        
      #3 List3 winding, living
      

      或使用base R

      aggregate(values ~ ind, stack(harvardList), 
        FUN = function(x) regmatches(x, regexpr(paste(IngWords, collapse="|"), x)))
      #  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")
      

      【讨论】:

        猜你喜欢
        • 2015-05-23
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        • 2019-08-10
        • 1970-01-01
        • 1970-01-01
        • 2018-07-06
        • 1970-01-01
        相关资源
        最近更新 更多