【发布时间】:2021-03-03 06:31:44
【问题描述】:
我有一个以下格式的大型 .txt 文件,显示大量用户的日期、用户和产品评论;
YYYY:MM:D1 @Username1: this is a product review
YYYY:MM:D1 @Username2: this is also a product review
YYYY:MM:D1 @Username3: this is also a product review that
runs to the next line
YYYY:MM:D1 @Username4: this here is also a product review
我想将其提取到具有 3 列的数据框,如下所示:
date/time username comment
yyyy/mm/dd @Username1 this is a product review
yyyy/mm/dd @Username2 this is also a product review
yyyy/mm/dd @Username3 this is also a product review contained in the same row
yyyy/mm/dd @Username4 this here is also a product review
使用标准的 R 基本命令
read.table("filename.txt", fill=TRUE)
给我一个数据框,它将产品评论中的每个单词视为不同的列。它还将评论变成足够长的“连续行”进入新行,即
V1 V2 V3 V4 V5
yy/mm/dd Username1 this is a
product review
...
任何帮助表示赞赏!
【问题讨论】:
-
列之间的空格是否只是一个空格字符(这是愚蠢的)?如果是这样,您只能在不带分隔符的情况下导入(即,作为一列)和
strsplit,然后使用正则表达式。 -
在
tidyr包中还有separate_rows()用于精确的字符串拆分,@Roland 描述了这一点。
标签: r dataframe text read.table