r - 从 R 中的 Excel 文件中清除文本(删除停用词、标点符号等)
问题描述
对于我的硕士论文,我正在分析一所大学的课程。我有 1134 门课程(作为行),有 3 个变量(作为列)。由于我对 RI 的经验很少,因此我在为它编写代码而苦苦挣扎。这是更多信息,在我有一个作为图像附加的数据库示例。
第 1 栏是课程名称 第 2 栏是课程描述 第 3 栏是学习成果
我想清理数据并删除停用词、标点符号和其他不相关的字符。我使用以下代码执行此操作:
rm(list=ls());
library(readxl);
library(MASS);
library(nnet);
library(NLP);
library(tm);
database <- read_excel("/Volumes/GoogleDrive/My Drive/TU e Innovation Management /Thesis/testdatabasematrix.xlsx");
#name columns
colnames(database)[1] <- "Name";
colnames(database)[2] <- "Description";
colnames(database)[3] <- "LearningOutcomes";
#replace punctuation
database2 <- gsub(pattern = "\\W", replace = " ", database)
#replace digits
database2 <- gsub(pattern="\\d", " ", database2)
#everything to lower
database2 <- tolower(database2)
#until here everything fine
database2 <- removeWords(database2, stopwords());
#When I try to save the database in a data frame, the output is merely 3 observations of 1 variable instead of 1141 obs. of 3 variables
database2 <- data.frame(database2)
我希望你能帮帮我 :)。如果您需要更多信息,请说出来,我当然会提供。
最好的,克里斯蒂安
解决方案
您也可以考虑tidytext
anddplyr
包,这非常好:
# some data similar to yours
database <- data.frame(Name = c('Aalto Fellows II', 'Aalto introduction to Services'),
Description = c('This course is a lot of words I do not know.','Service economy, whatever it does mean.'),
LearningOutcomes = c('Aalto Fellows, which are the smartest, learn.','Knowing what does Service economy means.'), stringsAsFactors = FALSE)
# cool packages
library(tidytext)
library(dplyr)
# here the text transformations for titles
title <- tibble(line = 1:nrow(database), text = database$Name) %>% # as tibble
unnest_tokens(word, text)%>% # remove punctuations, lowercase, put words in column
anti_join(stop_words, by = c("word" = "word")) %>% # remove stopwords
group_by(line) %>% summarise(title = paste(word,collapse =' ')) # now all in a row!
# here the text transformations for descriptions
description <- tibble(line = 1:nrow(database), text = database$Description) %>%
unnest_tokens(word, text) %>%
anti_join(stop_words, by = c("word" = "word")) %>%
group_by(line) %>% summarise(title = paste(word,collapse =' '))
# here the text transformations for learning outcomes
learningoutcomes <- tibble(line = 1:nrow(database), text = database$LearningOutcomes) %>%
unnest_tokens(word, text) %>%
anti_join(stop_words, by = c("word" = "word")) %>%
group_by(line) %>% summarise(title = paste(word,collapse =' '))
# now the full dataset
database2 <- title %>% left_join(description, by = 'line') %>% left_join(learningoutcomes, by = 'line')
colnames(database2) <- c("line","Name","Description","LearningOutcomes")
database2
# A tibble: 2 x 4
line Name Description LearningOutcomes
<int> <chr> <chr> <chr>
1 1 aalto fellows ii lot words aalto fellows smartest learn
2 2 aalto introduction services service economy knowing service economy means
您可以将其转换为带有data.frame()
.
推荐阅读
- pyspark - 如何在pyspark中的groupby之后对组中的每一行应用函数?
- python - 使用 Scrapy 遍历表行
- javascript - 乘以字符串的点
- sql - SSRS 不显示列和值
- objective-c - Objective-C,scheduledTimerWithTimeInterval 渐进式增加
- javascript - 如何检查js数组中是否存在字符串?
- amazon-web-services - 我可以得到一个 aws lambda 来运行 rspec 测试吗?
- android - NetworkBoundResource:为什么在 `ApiEmptyResponse` 中使用 `loadFromDb()`
- rust - 如何将生命周期检查推迟到运行时
- javascript - React 在我的 Flask 应用程序的索引页面中不起作用?