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
1条答案
按热度按时间jtjikinw1#
下面是一些示例数据和代码,以帮助您入门(并带有注解,以指导您完成代码):
创建于2023-04-15带有reprex v2.0.2