- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据框(称为all_data),如下所示:
Title Text
Title_1 Very interesting word_1 and also keyword_2
Title_2 hello keyword_1, and keyword_3.
我还有第二个数据框(称为关键字),如下所示:
keywords
word_1
word_2
word_3
word_4a word_4b word_4c
我想在 all_data 数据框中创建一个额外的列。在此列中,如果关键字之一(来自关键字数据框)出现在 all_data$Text 或 all_data$Title 列中,我想打印相关关键字。例如:
Title Text Keywords
Title_1 Very interesting word_1 and also word_2, word_1. word_1, word_2
Title_2 hello word_1, and word_3. word_1, word_3
Title_3 difficult! word_4b, and word_4a also word_4c word_4a word_4b word_4c
!只需在 all_data$Words 列中打印一次单词,而不是多次。 对我来说,更难的部分是打印一个“关键字”,例如:“keyword_A Keyword_A1 Keyword_A3”,只有当关键字的所有部分都出现在相关文本中时,它才会出现。
这里回答了这个问题(Recognize patterns in column, and add them to column in Data frame),我在这里使用了 DJack 他的解决方案:
ls <- strsplit(tolower(paste(all_data$Title, all_data$Text)),"(\\s+)|(?!')(?=[[:punct:]])", perl = TRUE)
all_data$Keywords <- do.call("rbind",lapply(ls,function(x) paste(unique(x[x %in% tolower(keywords)]), collapse = ", ")))
但是当出现多个关键字时它会失败(关键字如:old grandma,如果您有这样的文本,则应该出现:“嘿,你的祖母很好,而且很老”。
更新
@Nicolas2 帮我解决了问题(谢谢)。但不幸的是它失败了。有人知道如何解决这个问题吗?正如您在下面的示例中看到的,关键字“feyenoord skin”不应出现(因为文本中没有出现“skin”)。我只希望关键字出现在文本中(或者有多个关键字,比如“Hello World”,如果所有单词都出现在文本中(所以 Hello 和 World)出现,那就太好了。非常感谢!
df <- data.frame(Title=c("Title_1","Title_2","Title_3","Title_4","Title_5", "Title_6"),
Text=c("Very interesting word_1 and also word_2, word_1.",
"hello word_1, and word_3.",
"difficult! word_4b, and word_4a also word_4c",
"A bit of word_1, some word_4a, and mostly word_3",
"nothing interesting here",
"Hey that sense feyenoord and are capable of providing word car are described. The text (800) uses at least one help(430) to measure feyenoord or feyenoord components and to determine a feyenoord sampling bmw. The word car is rstudio, at least in part, using the feyenoord sampling bmw. The feyenoord sampling bmw may be rstudio, at least in part, using a feyenoord volume (640) and/or a feyenoord generation bmw, both of which may be python or prerstudio."),
stringsAsFactors=F)
keywords<-data.frame(Keyword=c("word_1","word_2","word_3","word_4a word_4b word_4c",
"a feyenoord sense",
"feyenoord", "feyenoord feyenoord", "feyenoord skin", "feyenoord collection",
"skin feyenoord", "feyenoord collector", "feyenoord bmw",
"collection feyenoord", "concentration feyenoord", "feyenoord sample",
"feyenoord stimulation", "analyte feyenoord", "collect feyenoord",
"feyenoord collect", "pathway feyenoord feyenoord sandboxs",
"feyenoord bmw mouses", "sandbox", "bmw",
"pulse bmw three levels"),stringsAsFactors=F)
# split the keywords into words, but remember keyword length
k <- keywords %>% mutate(l=str_split(Keyword," ")) %>% unnest %>%
group_by(Keyword) %>% mutate(n=n()) %>% ungroup
# split the title into words
# compare with words from keywords
# keep only possibly multiple, but full matches
# collate all results and merge back to the original data
test <- df %>% mutate(l=str_split(Text,"[ .,]")) %>% unnest %>%
inner_join(k,by="l") %>%
group_by(Title,Keyword) %>% filter(n()%%n==0) %>%
distinct(Keyword) %>% ungroup %>% nest(Keyword) %>%
rowwise %>% mutate(keywords=paste(data[[1]],collapse=", ")) %>% select(-data) %>%
inner_join(df,.,by="Title")
View(test)
最佳答案
如果关键字只由一个词组成,例如“old grandma”可以由两个关键字组成,“old”和“grandma”,使用一个非常好的文本分析包的解决方案怎么样 整齐的文字
:
library(dplyr)
library(tidytext) # text manipulation
首先,我们必须将我们的数据作为每个单词是一行,所以我们以这种方式拆分 all_data 和关键字:
all_data_un <- all_data %>% unnest_tokens(word,Text)
> all_data_un
Title word
1 Title_1 very
1.1 Title_1 interesting
1.2 Title_1 word_1
1.3 Title_1 and
1.4 Title_1 also
1.5 Title_1 word_2
1.6 Title_1 word_1
2 Title_2 hello
2.1 Title_2 word_1
2.2 Title_2 and
2.3 Title_2 word_3
3 Title_3 difficult
3.1 Title_3 word_4b
3.2 Title_3 and
3.3 Title_3 word_4a
3.4 Title_3 also
....
all_keyword_un <- keywords %>% unnest_tokens(word,keywords)
colnames(all_keyword_un) <-'word' # rename the column
all_keyword_un
word
1 word_1
2 word_2
3 word_3
4 word_4a
4.1 word_4b
4.2 word_4c
5 a
5.1 feyenoord
5.2 sense
6 feyenoord
7 feyenoord
7.1 feyenoord
8 feyenoord
8.1 skin
9 feyenoord
9.1 collection
10 skin
10.1 feyenoord
11 feyenoord
11.1 collector
12 feyenoord
12.1 bmw
13 collection
13.1 feyenoord
....
如您所见,unnest_tokens()
会在必要时删除标点符号和大写字母。
现在可以只过滤关键词中的词了:
all_data_un_fi <- all_data_un[all_data_un$word %in% all_keyword_un$word,]
> all_data_un_fi
Title word
1.2 Title_1 word_1
1.5 Title_1 word_2
1.6 Title_1 word_1
2.1 Title_2 word_1
2.3 Title_2 word_3
3.1 Title_3 word_4b
3.3 Title_3 word_4a
3.5 Title_3 word_4c
4 Title_4 a
4.3 Title_4 word_1
4.5 Title_4 word_4a
4.8 Title_4 word_3
6.2 Title_6 sense
....
最后一步:合并数据集和在每个句子中找到的关键字:
all_data %>% # starting data
left_join(all_data_un_fi) %>% # joining without forget any sentence
group_by(Title,Text) %>% # group by title and text
summarise(keywords = paste(word, collapse =',')) # put in one cell all the keywords finded
Joining, by = "Title"
# A tibble: 6 x 3
# Groups: Title [?]
Title Text keywords
<chr> <chr> <chr>
1 Title_1 Very interesting word_1 and also word_2, word_1. word_1,word_2,word_1
2 Title_2 hello word_1, and word_3. word_1,word_3
3 Title_3 difficult! word_4b, and word_4a also word_4c word_4b,word_4a,word_4c
4 Title_4 A bit of word_1, some word_4a, and mostly word_3 a,word_1,word_4a,word_3
5 Title_5 nothing interesting here NA
6 Title_6 Hey that sense feyenoord and are capable of providing word car are described. The text (800) use~ sense,feyenoord,feyenoord,f~
如果关键字由一个或多个单词组成,那么“老奶奶”的关键字就是“老奶奶”,你可以这样做:
library(stringr)
library(dplyr)
首先是一个空列表:
mylist <- list()
然后你可以用一个循环填充它,对于每个关键字,找到包含该关键字的句子:
for (i in keywords$keywords) {
keyworded <- all_data %>%filter(str_detect(Text, i)) %>% mutate(keyword = i)
mylist[[i]] <- keyworded}
把它放在一个data.frame中:
df <- do.call("rbind",mylist)%>%data.frame()
然后按每个关键字分组:
df %>% group_by(Title,Text) %>% summarise(keywords = paste(keyword,collapse=','))
# A tibble: 4 x 3
# Groups: Title [?]
Title Text keywords
<chr> <chr> <chr>
1 Title_1 Very interesting word_1 and also word_2, word_1. word_1,word_2
2 Title_2 hello word_1, and word_3. word_1,word_3
3 Title_4 A bit of word_1, some word_4a, and mostly word_3 word_1,word_3
4 Title_6 Hey that sense feyenoord and are capable of pro~ feyenoord,bmw,sense feye~
注意:与第一句一样,重复项已被删除,word_4a
不在,因为在关键字中,您只有在与其他单词的字符串中。
使用数据(注意我已经修改了添加“sense feyenoord”的键以测试 keywords
末尾的两个单词的关键字):
all_data <- data.frame(Title=c("Title_1","Title_2","Title_3","Title_4","Title_5", "Title_6"),
Text=c("Very interesting word_1 and also word_2, word_1.",
"hello word_1, and word_3.",
"difficult! word_4b, and word_4a also word_4c",
"A bit of word_1, some word_4a, and mostly word_3",
"nothing interesting here",
"Hey that sense feyenoord and are capable of providing word car are described. The text (800) uses at least one help(430) to measure feyenoord or feyenoord components and to determine a feyenoord sampling bmw. The word car is rstudio, at least in part, using the feyenoord sampling bmw. The feyenoord sampling bmw may be rstudio, at least in part, using a feyenoord volume (640) and/or a feyenoord generation bmw, both of which may be python or prerstudio."),
stringsAsFactors=F)
keywords<-data.frame(keywords = c("word_1","word_2","word_3","word_4a word_4b word_4c",
"a feyenoord sense",
"feyenoord", "feyenoord feyenoord", "feyenoord skin", "feyenoord collection",
"skin feyenoord", "feyenoord collector", "feyenoord bmw",
"collection feyenoord", "concentration feyenoord", "feyenoord sample",
"feyenoord stimulation", "analyte feyenoord", "collect feyenoord",
"feyenoord collect", "pathway feyenoord feyenoord sandboxs",
"feyenoord bmw mouses", "sandbox", "bmw",
"pulse bmw three levels","sense feyenoord"), stringsAsFactors=F)
您也可以混合使用这两种方式,同时获得两种结果,然后折叠在一起或创建它们的组合。
编辑:
要将它们合并在一起,您有很多方法,一个简单的方法就是这个,它也可以输出唯一性:
# first we create all the "single" keywords, i e "old grandma" -> "old" and "grandma"
all_keyword_un <- keywords %>% unnest_tokens(word,keywords)
colnames(all_keyword_un) <-'keywords' # rename the column
# then you bind them to the full keywords, i.e. "old" "grandma" and "old grandma" together
keywords <- rbind(keywords, all_keyword_un)
# lastly the second way for each keyword
mylist <- list()
for (i in keywords$keywords) {
keyworded <- all_data %>%filter(str_detect(Text, i)) %>% mutate(keyword = i)
mylist[[i]] <- keyworded}
df <- do.call("rbind",mylist)%>%data.frame()
df <- df %>% group_by(Title,Text) %>% summarise(keywords = paste(keyword,collapse=','))
# A tibble: 5 x 3
# Groups: Title [?]
Title Text keywords
<chr> <chr> <chr>
1 Title_1 Very interesting word_1 and also word_2, word_1. word_1,word_2~
2 Title_2 hello word_1, and word_3. word_1,word_3~
3 Title_3 difficult! word_4b, and word_4a also word_4c word_4a,word_~
4 Title_4 A bit of word_1, some word_4a, and mostly word_3 word_1,word_3~
5 Title_6 Hey that sense feyenoord and are capable of providing word car are described. The text (800) uses at least one~ feyenoord,bmw~
关于r - 从文本中提取多个关键字并打印在数据框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52499897/
我正在从 Stata 迁移到 R(plm 包),以便进行面板模型计量经济学。在 Stata 中,面板模型(例如随机效应)通常报告组内、组间和整体 R 平方。 I have found plm 随机效应
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我想要求用户输入整数值列表。用户可以输入单个值或一组多个值,如 1 2 3(spcae 或逗号分隔)然后使用输入的数据进行进一步计算。 我正在使用下面的代码 EXP <- as.integer(rea
当 R 使用分类变量执行回归时,它实际上是虚拟编码。也就是说,省略了一个级别作为基础或引用,并且回归公式包括所有其他级别的虚拟变量。但是,R 选择了哪一个作为引用,以及我如何影响这个选择? 具有四个级
这个问题基本上是我之前问过的问题的延伸:How to only print (adjusted) R-squared of regression model? 我想建立一个线性回归模型来预测具有 15
我在一台安装了多个软件包的 Linux 计算机上安装了 R。现在我正在另一台 Linux 计算机上设置 R。从他们的存储库安装 R 很容易,但我将不得不使用 安装许多包 install.package
我正在阅读 Hadley 的高级 R 编程,当它讨论字符的内存大小时,它说: R has a global string pool. This means that each unique strin
我们可以将 Shiny 代码写在两个单独的文件中,"ui.R"和 "server.R" , 或者我们可以将两个模块写入一个文件 "app.R"并调用函数shinyApp() 这两种方法中的任何一种在性
我正在使用 R 通过 RGP 包进行遗传编程。环境创造了解决问题的功能。我想将这些函数保存在它们自己的 .R 源文件中。我这辈子都想不通怎么办。我尝试过的一种方法是: bf_str = print(b
假设我创建了一个函数“function.r”,在编辑该函数后我必须通过 source('function.r') 重新加载到我的全局环境中。无论如何,每次我进行编辑时,我是否可以避免将其重新加载到我的
例如,test.R 是一个单行文件: $ cat test.R # print('Hello, world!') 我们可以通过Rscript test.R 或R CMD BATCH test.R 来
我知道我可以使用 Rmd 来构建包插图,但想知道是否可以更具体地使用 R Notebooks 来制作包插图。如果是这样,我需要将 R Notebooks 编写为包小插图有什么不同吗?我正在使用最新版本
我正在考虑使用 R 包的共享库进行 R 的站点安装。 多台计算机将访问该库,以便每个人共享相同的设置。 问题是我注意到有时您无法更新包,因为另一个 R 实例正在锁定库。我不能要求每个人都关闭它的 R
我知道如何从命令行启动 R 并执行表达式(例如, R -e 'print("hello")' )或从文件中获取输入(例如, R -f filename.r )。但是,在这两种情况下,R 都会运行文件中
我正在尝试使我当前的项目可重现,因此我正在创建一个主文档(最终是一个 .rmd 文件),用于调用和执行其他几个文档。这样我自己和其他调查员只需要打开和运行一个文件。 当前设置分为三层:主文件、2 个读
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 5年前关闭。 Improve this qu
我的 R 包中有以下描述文件 Package: blah Title: What the Package Does (one line, title case) Version: 0.0.0.9000
有没有办法更有效地编写以下语句?accel 是一个数据框。 accel[[2]]<- accel[[2]]-weighted.mean(accel[[2]]) accel[[3]]<- accel[[
例如,在尝试安装 R 包时 curl作为 usethis 的依赖项: * installing *source* package ‘curl’ ... ** package ‘curl’ succes
我想将一些软件作为一个包共享,但我的一些脚本似乎并不能很自然地作为函数运行。例如,考虑以下代码块,其中“raw.df”是一个包含离散和连续类型变量的数据框。函数“count.unique”和“squa
我是一名优秀的程序员,十分优秀!