【问题标题】:Using tm package in R to clean the columns in dataframe在 R 中使用 tm 包清理数据框中的列
【发布时间】:2018-07-10 17:40:48
【问题描述】:

我希望使用 tm 包来更改数据帧的列,即我想使用 content_transformer、removePunctuation 等函数应用于数据帧的列。

例如使用下面的数据框

df <- data.frame(a=c("I love TEXTMINING","Here I GO, Again!!"))

我希望我们使用 content_transformer 将 df$a 变为小写,并使用 removePunctuation 删除标点符号,使输出如下所示

                  a
1 i love textmining
2   here i go again

有没有办法专门使用 tm 包中的函数来执行上述操作?

【问题讨论】:

  • 您可以尝试正则表达式 df$a &lt;- gsub("[[:punct:]]+", "", tolower(df$a))tm tolower(removePunctuation(as.character(df$a)))#[1] "i love textmining" "here i go again"

标签: r string tm


【解决方案1】:

这里以使用 tm 包为例:

df <- data.frame(a=c("I love TEXTMINING","Here I GO, Again!!"))

library(tm)
corpus<-Corpus(VectorSource(df$a))
corpus<-tm_map(corpus, removeNumbers)
corpus<-tm_map(corpus, content_transformer(tolower))
#corpus<-tm_map(corpus, removeWords, stopwords('english'))
corpus<-tm_map(corpus, removePunctuation)

answer<-unlist(as.list(corpus))
answer

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 2020-06-05
  • 2012-08-24
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多