首页 > 解决方案 > 从 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)

我希望你能帮帮我 :)。如果您需要更多信息,请说出来,我当然会提供。

最好的,克里斯蒂安

标签: rtext

解决方案


您也可以考虑tidytextanddplyr包,这非常好:

# 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().


推荐阅读