将每个提取的评论分配给单行 write.table R 数据框

Assign each extracted comment to a single row write.table R data frame

首先,我是一个编码菜鸟,刚开始编码是为了在我的大学写硕士论文。我使用 R 中的 tuber 包提取了 youtube 评论,以便对这些评论进行情感分析。一切正常,我收到了一个包含所有评论的数据框(11314 个观察值和 13 个变量)。但是,当我尝试编写该数据框的 .csv 文件以查看 Excel 中的注释时,我遇到了一个特殊问题。对于包含新段落的评论,write.table 函数创建了一个新行。我使用了以下功能:

write.table(testneuohneduplikate, file = "Testneuohnedulikate.csv",sep = ";", row.names = FALSE, col.names = TRUE, quote = TRUE)

是否有可能每条评论都写在一行中,而不是有时两行或三行,因为评论包含段落?

我希望我能够正确解释我的问题。

提前谢谢你们,来自德国的问候无论你们来自哪里:)

是的,write.table 在遇到换行符时正在创建一个新行。下面是从注释字符串中删除换行符的示例:

> comment<-"I think this video \n is great"
> cat(comment)
I think this video 
 is great

> fixedcomment<-gsub("[\r\n]", "", comment)
> cat(fixedcomment)
I think this video  is great
> 

您可以使用 'apply' 将其应用于 table 中的每一列,或者如果您只想处理行或列,则修改 MARGIN 参数。

> ID<-1:4
> Names<-c('name1','name2','name3','name4')
> Comments<-c("I think this video \n is great", "No it stinks \n I mean it", "Use the Force", "It's time \n to get to work")
> table<-cbind(ID, Names, Comments)

> fixed_table<-apply(X=table,MARGIN=c(1,2),FUN = function(y) gsub("[\r\n]","",y))