【问题标题】:R - How do document the number of grepl matches based in another data frame?R - 如何在另一个数据框中记录 grepl 匹配的数量?
【发布时间】:2016-06-11 13:12:58
【问题描述】:

这确实是一个相当棘手的问题。如果有人能够帮助我,那就太棒了。

我正在尝试做的是以下内容。我在 R 中有一个数据框,其中包含给定状态下的每个位置,从 Wikipedia 中抓取。它看起来像这样(前 10 行)。我们就叫它NewHampshire.df

 Municipality       County Population
1       Acworth     Sullivan        891
2        Albany      Carroll        735
3    Alexandria      Grafton       1613
4    Allenstown    Merrimack       4322
5       Alstead     Cheshire       1937
6         Alton      Belknap       5250
7       Amherst Hillsborough      11201
8       Andover    Merrimack       2371
9        Antrim Hillsborough       2637
10      Ashland      Grafton       2076

我进一步编译了一个名为grep_term 的新变量,它将MunicipalityCounty 中的值组合成一个新的变量,用作or 语句,如下所示:

 Municipality       County Population  grep_term
1       Acworth     Sullivan        891  "Acworth|Sullivan"
2       Albany      Carroll        735   "Albany|Carroll"

等等。此外,我还有另一个数据集,其中包含 2000 个 Twitter 用户的自我披露位置。我叫它location.df,看起来有点像这样:

[1] "London"                     "Orleans village VT USA"     "The World"                 
 [4] "D M V Towson "              "Playa del Sol Solidaridad"  "Beautiful Downtown Burbank"
 [7] NA                           "US"                         "Gaithersburg Md"           
[10] NA                           "California "                "Indy"                      
[13] "Florida"                    "exsnaveen com"              "Houston TX"    

我想做两件事:

1:对location.df 数据集中的每个观察结果进行Grepl,并将TRUE 或FALSE 保存到新变量中,具体取决于自我披露的位置是否在第一个数据集中的列表中。

2:将NewHampshire.df 数据集中特定行的匹配数保存到新变量中。即,如果 twitter 位置数据集中有 4 个 Acworth 匹配项,则 NewHampshire.df 中新创建的“matches”变量上的观察值 1 应该有一个值“4”

到目前为止我做了什么:我已经解决了任务1,如下:

for(i in 1:234){
  location.df$isRelevant <- sapply(location.df$location, function(s) grepl(NH_Places[i], s, ignore.case = TRUE))
}

如何解决任务 2,最好是在同一个 for 循环中?

提前致谢,任何帮助将不胜感激!

【问题讨论】:

  • 您用于解决任务 1 的代码将写入 location.df$isRelevant 234 次,只有最后写入的代码才会保留。因此,删除循环并将 i 替换为 234 将得到相同的结果,这可能不是您想要的。

标签: regex r twitter


【解决方案1】:

关于任务一,你也可以使用:

# location vector to be matched against
loc.vec <- c("Acworth","Hillsborough","California","Amherst","Grafton","Ashland","London")
location.df <- data.frame(location=loc.vec)

# create a 'grep-vector'
places <- paste(paste(NewHampshire$Municipality, NewHampshire$County,
                      sep = "|"), 
                collapse = "|")
# match them against the available locations
location.df$isRelevant <- sapply(location.df$location, 
                                 function(s) grepl(places, s, ignore.case = TRUE))

给出:

> location.df
      location isRelevant
1      Acworth       TRUE
2 Hillsborough       TRUE
3   California      FALSE
4      Amherst       TRUE
5      Grafton       TRUE
6      Ashland       TRUE
7       London      FALSE

要获取location.dfgrep_term 列的匹配数,您可以使用:

NewHampshire$n.matches <- sapply(NewHampshire$grep_term, function(x) sum(grepl(x, loc.vec)))

给予:

> NewHampshire
   Municipality       County Population            grep_term n.matches
1       Acworth     Sullivan        891     Acworth|Sullivan         1
2        Albany      Carroll        735       Albany|Carroll         0
3    Alexandria      Grafton       1613   Alexandria|Grafton         1
4    Allenstown    Merrimack       4322 Allenstown|Merrimack         0
5       Alstead     Cheshire       1937     Alstead|Cheshire         0
6         Alton      Belknap       5250        Alton|Belknap         0
7       Amherst Hillsborough      11201 Amherst|Hillsborough         2
8       Andover    Merrimack       2371    Andover|Merrimack         0
9        Antrim Hillsborough       2637  Antrim|Hillsborough         1
10      Ashland      Grafton       2076      Ashland|Grafton         2

【讨论】:

  • 您好,非常感谢您的回答。我尝试了您对任务 1 的方法,但由于某种原因,除了 NA 之外,它为每个值都给了我一个“TRUE”。怎么可能?
  • London TRUE 2 Orleans village VT USA TRUE 3 The World TRUE 4 D M V Towson TRUE 5 Playa del Sol Solidaridad TRUE 6 Beautiful Downtown Burbank TRUE 7 NA FALSE 8 US TRUE 9 Gaithersburg Md TRUE 10 NA FALSE 11 California TRUE 12 Indy TRUE 13 Florida TRUE 14 exsnaveen com TRUE 15 Houston TX TRUE 16 Tweaking TRUE 17 Phoenix AZ TRUE 18 Malibu Ca USA TRUE 19 Hermosa Beach CA TRUE 20 California USA TRUE 21 Here TRUE
  • @nikUoM 我的第一个猜测是所有这些名称也在另一个数据框中。因为它在我使用的示例中有效,所以如果没有您使用的数据,很难说问题出在哪里。
  • @nikUoM 您能否在问题中包含示例数据来说明您的评论中的问题?
猜你喜欢
  • 1970-01-01
  • 2021-09-17
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多