如果答案很简单,我很抱歉;我对R不熟悉,但找不到解决办法
我有一个数据框架,我感兴趣的列(“subjects”)每一行都有一个单词列表。
title subject
----- -----------------------------------------------
A c("health sciences", "life sciences")
B c("biochemistry", "medicine", "life sciences")
C c("physics and astronomy", "mathematics")我想用“生物学”代替(因此分类)所有与生物学相关的单词。因此,基本上,如果任何标题都有一个与生物学相关的主题列表,那么它们的主题将被更简单的“生物学”所取代。
所以我的数据框架是这样的:
title subject
----- -----------------------------------------------
A biology
B biology
C c("physics and astronomy", "mathematics")我如何将所有以关键前缀(如“生物”、“健康”、“医学”、“生命”等)开头的单词替换为“生物学”?
发布于 2018-03-21 15:35:08
尝试修改代码:
玩具data.frame
df<-data.frame(title=c("A","B"),subject=c("health sciences", "other stuffs"))解决办法:
toMatch<-c("^bio","^health", "^med", "^life")
df$subject<-as.character(df$subject)
df[grepl(paste(toMatch,collapse="|"),df$subject ),"subject"]<-"biology"你的产出:
df
title subject
1 A biology
2 B other stuffshttps://stackoverflow.com/questions/49410362
复制相似问题