针对R中文本中的特定单词标记文本数据集

针对R中文本中的特定单词标记文本数据集,r,text-analysis,R,Text Analysis,我不熟悉R,但我有一种情况,我需要创建新的变量标志,并将它标记为1,与文本中的特定单词相关。 示例:数据帧 Text flag_USA flag_Canada Canada has 1.6% more total area 0 1 USA has 0.7% more land 1 0 USA ha

我不熟悉R,但我有一种情况,我需要创建新的变量标志,并将它标记为1,与文本中的特定单词相关。 示例:数据帧

Text                                        flag_USA    flag_Canada
Canada has 1.6% more total area                  0         1
USA has 0.7% more land                           1         0
USA has 4 times more arable land in total        1         0
Canada has 27.5% more forested and wooded land   0         1
USA has 26.9 times more irrigated land           1         0
所以我想在文本中有美国或加拿大的地方创建一个flagvariable。
请您帮助我获取此代码。提前感谢您的宝贵建议。

使用
grepl
函数,如果在字符串中找到模式,grepl将返回
TRUE
,如果没有,则返回
FALSE

您的代码可能如下所示:

df$flag_USA    <- grepl("USA",    df$Text)
df$flag_Canada <- grepl("Canada", df$Text)

df$flag_USA我们还可以使用
regexpr/regmatches
提取单词,并使用
binary
将其转换为二进制列。当有许多关键字并且不想重复执行
grepl

df1[c('flag_USA', 'flag_Canada')] <- table(1:nrow(df1), 
     factor(regmatches(df1$Text, regexpr('USA|Canada', df1$Text)), 
                levels=c('USA', 'Canada')))
df1
#                                             Text flag_USA flag_Canada
#1                Canada has 1.6% more total area        0           1
#2                         USA has 0.7% more land        1           0
#3      USA has 4 times more arable land in total        1           0
#4 Canada has 27.5% more forested and wooded land        0           1
#5         USA has 26.9 times more irrigated land        1           0

df1[c('flag_USA','flag_Canada')]这是使用quanteda包将字典应用于文本的完美任务。首先,使用模式定义一个字典(可以使用“全局”匹配、固定格式或正则表达式进行匹配——请参见
?dfm
?applyDictionary
),然后在文本中使用此模式,使用
dfm()
dictionary
参数创建文档特征矩阵

> txt <- c("Canada has 1.6% more total area",
+          "USA has 0.7% more land",
+          "USA has 4 times more arable land in total",
+          "Canada has 27.5% more forested and wooded land",
+          "USA has 26.9 times more irrigated land")
> require(quanteda)
> myDictionary <- dictionary(list(flag_USA = "USA", flag_Canada = "Canada"))
> dfm(txt, dictionary = myDictionary)
Creating a dfm from a character vector ...
   ... lowercasing
   ... tokenizing
   ... indexing documents: 5 documents
   ... indexing features: 14 feature types
   ... applying a dictionary consisting of 2 keys
   ... created a 5 x 2 sparse dfm
   ... complete. 
Elapsed time: 0.014 seconds.
Document-feature matrix of: 5 documents, 2 features.
5 x 2 sparse Matrix of class "dfmSparse"
       features
docs    flag_USA flag_Canada
  text1        0           1
  text2        1           0
  text3        1           0
  text4        0           1
  text5        1           0
>txt require(quanteda)
>myDictionary dfm(txt,dictionary=myDictionary)
从字符向量创建dfm。。。
... 小写
... 标志法
... 索引文件:5个文件
... 索引特征:14种特征类型
... 应用由两个键组成的字典
... 创建了一个5 x 2稀疏dfm
... 完成
运行时间:0.014秒。
文档特征矩阵:5个文档,2个特征。
“dfmSparse”类的5 x 2稀疏矩阵
特征
美国国旗加拿大国旗
文本10 1
文本2 1 0
文本3 1 0
文本4 0 1
文本5 1 0

您自己已经尝试过的东西?因此,它不是一个代码编写服务。提示:查看
?grepl