我正在学习使用R,所以请多多包涵。
我有一个Google Play商店应用(master_tib)的数据集。每行都是一个Play商店应用。标题为描述的列包含有关应用程序功能的文本。
master_tib
App Description
App1 Reduce your depression and anxiety
App2 Help your depression
App3 This app helps with Anxiety
App4 Dog walker app 3000
我还有一个df标签(master_tags),其中包含我预定义的重要单词。只有一列标题为标签的标签,每行包含一个标签。
master_tag
Tag
Depression
Anxiety
Stress
Mood
我的目标是根据说明中标记的存在,使用master_tags df中的标记来标记master_tib df中的应用。然后,它将在新列中打印标签。最终结果将是一个master_tib df,如下所示:
App Description Tag
App1 Reduce your depression and anxiety depression, anxiety
App2 Help your depression depression
App3 This app helps with anxiety anxiety
App4 Dog walker app 3000 FALSE
下面是我到目前为止结合使用str_detect和mapply所做的事情:
# define function to use in mapply
detect_tag <- function(description, tag){
if(str_detect(description, tag, FALSE)) {
return (tag)
} else {
return (FALSE)
}
}
index <- mapply(FUN = detect_tag, description = master_tib$description, master_tags$tag)
master_tib[index,]
不幸的是,只有第一个标签正在通过。
App Description Tag
App1 Reduce your depression and anxiety depression
而不是所需的:
App Description Tag
App1 Reduce your depression and anxiety depression, anxiety
我还没有将结果打印到新列中。很想听听任何人的见解或想法,并为我糟糕的R技能提前道歉。
您可以使用master_tag
组合str_c
中的单词,然后使用str_extract_all
获得所有与模式匹配的单词。
使用tidyverse
(dplyr
,stringr
,tidyr
)中的多个包以及@Ronak Shah的答案中显示的数据。首先将标签转换为模式:
类似于@RonakShah的答案,但底数为R: