如何写一个R代码来搜索关键字?

qyzbxkaa  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(141)

我的目标是调查:文章的标题或摘要是否包含关键字,并以某种方式选择或标记这些文章(创建一个新的类别,如包含/不包含任何关键字列表).我收集了这些文章在Excel文件中,我有它的标题,摘要那里.在这种情况下,需要考虑的步骤是什么?顺便说一句,我没有什么经验,刚刚开始学习R,我来自心理学领域.
我还没开始,只是在想怎么解决

jtjikinw

jtjikinw1#

下面是一些示例数据和代码,以帮助您入门(并带有注解,以指导您完成代码):

library(tidyverse)

# define sample data
df <- tibble(
  title = c("Programming in R", "How to post on StackOverflow", "A pasta recipe"),
  abstract = c("This is an article about reproducible resesarch.", "Have a question on coding? Learn how to ask anything.", "Ingredients: 2 eggs, a little flour.")
)

# define keyword
keywords <- "programming|coding"

# create indicator column that is TRUE if either title or abstract contain any keyword
df |>
  mutate(about_programming = str_detect(title, regex(keywords, ignore_case = TRUE)) |
           str_detect(abstract, regex(keywords, ignore_case = TRUE)))
#> # A tibble: 3 × 3
#>   title                        abstract                        about_programming
#>   <chr>                        <chr>                           <lgl>            
#> 1 Programming in R             This is an article about repro… TRUE             
#> 2 How to post on StackOverflow Have a question on coding? Lea… TRUE             
#> 3 A pasta recipe               Ingredients: 2 eggs, a little … FALSE

创建于2023-04-15带有reprex v2.0.2

相关问题