Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用时间戳将一个文件一分为二_R_File - Fatal编程技术网

R 使用时间戳将一个文件一分为二

R 使用时间戳将一个文件一分为二,r,file,R,File,具有以下格式的txt文件: 2011-01-01 00:00:00 text text text text 2011-01-01 00:01:00 text text text text text 2011-01-01 00:02:00 text text text text .... .... .... .... 2011-01-02 00:00:00 text text text text 2011-01-02 00:01:00 text text text text

具有以下格式的txt文件:

 2011-01-01 00:00:00 text text text text
 2011-01-01 00:01:00 text text text text
 text
 2011-01-01 00:02:00 text text text text
 ....
 ....
 ....
 ....
 2011-01-02 00:00:00 text text text text
 2011-01-02 00:01:00 text text text text
所有文件都包含两个日历日的数据。
是否可以将文件分成两个不同的文件,每天一个?

您必须阅读文件的所有行

您可以尝试使用

library(package=reshape)
然后函数
read.table
可能会有所帮助


然后您必须比较所有行并将它们写回两个新文件中

您必须读取文件中的所有行

您可以尝试使用

library(package=reshape)
然后函数
read.table
可能会有所帮助


然后您必须比较所有行并将它们写回两个新文件中

使用read.table()读取数据

我们的data.frame应类似于:

df <- data.frame(d = c("2011-01-01 00:00:00", "2011-01-01 00:01:00"), x = 0:1)

使用read.table()读取中的数据

我们的data.frame应类似于:

df <- data.frame(d = c("2011-01-01 00:00:00", "2011-01-01 00:01:00"), x = 0:1)

dat
dat您想要R中的数据吗?或者您只是想分割文件?将它们分割并保存到两个不同的txt文件中文件是否已排序?是否有一个标题需要复制到两个文件中?是否要将数据复制到R中?或者您只是想分割文件?将它们分割并保存到两个不同的txt文件中文件是否已排序?是否有一个标题需要复制到两个文件中?这是一个评论/建议,不是答案。这是一个评论/建议,不是答案。请注意,OP似乎希望按日期分割,而不是按日期时间分割。请注意,OP似乎希望按日期分割,而不是按日期时间分割。
dat <- readLines(textConnection(" 2011-01-01 00:00:00 text text text text
  2011-01-01 00:01:00 text text text text  text
  2011-01-01 00:02:00 text text text text
  2011-01-02 00:00:00 text text text text
  2011-01-02 00:01:00 text text text text"))

grouped.lines <- split(dat, substr(dat, 1,11) )
grouped.lines
$` 2011-01-01`
[1] " 2011-01-01 00:00:00 text text text text"      
[2] " 2011-01-01 00:01:00 text text text text  text"
[3] " 2011-01-01 00:02:00 text text text text"      

$` 2011-01-02`
[1] " 2011-01-02 00:00:00 text text text text"
[2] " 2011-01-02 00:01:00 text text text text"