我想阅读文本文件。
文件内容如下所示。
2013-08-13 19:26:58 Method for modifying a piece of 3D geometry
2013-08-13 19:26:57 Method of interactively modifying a feature
...
我想像这张表一样阅读这个文件
dateTime Method
"2013-08-13 19:26:58" "Method for modifying a piece of 3D geometry"
"2013-08-13 19:26:57" "Method of interactively modifying a feature"
...
如您所见,我想将该行与两个连续的空格(“\ s \ s”)分隔,而不是一个空格。
我该怎么做?
我尝试使用read.table函数,但允许使用一个字符分隔符。
或者我可以在没有第一列的情况下阅读文件内容吗?
像这样。"Method for modifying a piece of 3D geometry"
"Method of interactively modifying a feature"
请给我一些建议。谢谢
答案 0 :(得分:3)
首先用任何sep字符替换双空格:
txt<-"2013-08-13 19:26:58 Method for modifying a piece of 3D geometry
2013-08-13 19:26:57 Method of interactively modifying a feature"
read.table(sep="|",text=gsub(" ","|",txt), header=F)
# V1 V2
#1 2013-08-13 19:26:58 Method for modifying a piece of 3D geometry
#2 2013-08-13 19:26:57 Method of interactively modifying a feature
答案 1 :(得分:1)
假设您的数据文件位于test.txt
:
txt <- readLines('test.txt')
do.call(rbind, strsplit(txt, ' '))
# or alternatively
do.call(rbind.data.frame, strsplit(txt, ' '))