【发布时间】:2021-11-16 05:44:55
【问题描述】:
我有包含来自任务的行为数据的文本文件。但是,每个文件的前 18 行都是描述性信息(日期、时间、ID 号等),都在一大块文本中。实际的列名/数据从第 19 行开始。不是一种理想的格式,但我必须保留。
在研究 readlines() 和 writelines() 函数时,我似乎需要将文本文件读入 R 以重新组织数据,然后将其作为具有相同文本块的文本文件写回在前 18 行中。我不确定这实际上是如何工作的-我是否需要以某种方式将readlines() 和read.delim() 结合起来,或者readlines() 是否也会在第18 行下读入我的所有数据,就像我要读到read.delim(location, skip=18) 一样?
作为参考,以下是我正在使用的文本文件的示例:
# Non-editable header begin --------------------------------------------------------------------------------
# data format...............: continuous
# setname...................: 200ICAready
# filename..................: none_specified
# filepath..................: none_specified
# nchan.....................: 29
# pnts......................: 666445
# srate.....................: 500
# nevents...................: 1792
# generated by (bdf)........:
# generated by (set)........: 200ICAready
# reported in ..............:
# prog Version..............: 7.0.0
# creation date.............: 10-Sep-2021 16:21:24
# user Account..............:
#
# Non-editable header end --------------------------------------------------------------------------------
# item bepoch ecode label onset diff dura b_flags a_flags enable bin
# (sec) (msec) (msec) (binary) (binary)
1 0 13 "" 9.9980 0.00 0.0 00000000 00000000 1 [ ]
2 0 4 "" 10.9990 1001.00 0.0 00000000 00000000 1 [ ]
3 0 10 "" 11.1990 200.00 0.0 00000000 00000000 1 [ ]
4 0 14 "" 11.3990 200.00 0.0 00000000 00000000 1 [ ]
5 0 13 "" 12.7320 1333.00 0.0 00000000 00000000 1 [ ]
6 0 1 "" 13.7320 1000.00 0.0 00000000 00000000 1 [ ]
7 0 7 "" 13.9320 200.00 0.0 00000000 00000000 1 [ ]
结果如下:
# Non-editable header begin --------------------------------------------------------------------------------
# data format...............: continuous
# setname...................: 200ICAready
# filename..................: none_specified
# filepath..................: none_specified
# nchan.....................: 29
# pnts......................: 666445
# srate.....................: 500
# nevents...................: 1792
# generated by (bdf)........:
# generated by (set)........: 200ICAready
# reported in ..............:
# prog Version..............: 7.0.0
# creation date.............: 10-Sep-2021 16:21:24
# user Account..............:
#
# Non-editable header end --------------------------------------------------------------------------------
# item bepoch ecode label onset diff dura b_flags a_flags enable bin
# (sec) (msec) (msec) (binary) (binary)
1 0 13 "" 9.9980 0.00 0.0 00000000 00000000 1 [ ]
2 0 4 "" 10.9990 1001.00 0.0 00000000 00000000 1 [ ]
3 0 10 "" 11.1990 200.00 0.0 00000000 00000000 1 [ ]
4 0 15 "" 11.2500 200.00 0.0 00000000 00000000 1 [ ]
5 0 14 "" 11.3990 200.00 0.0 00000000 00000000 1 [ ]
6 0 13 "" 12.7320 1333.00 0.0 00000000 00000000 1 [ ]
7 0 1 "" 13.7320 1000.00 0.0 00000000 00000000 1 [ ]
8 0 19 "" 13.9320 200.00 0.0 00000000 00000000 1 [ ]
所以,我需要 R 在处理数据时临时存储不可编辑的标题部分,然后将其写为包含标题的文本文件。
编辑:我分别读取了标题和数据文件,现在正试图找到一种正确合并它们的方法。 c(header, datafile) 和 merge(header, datafile) 不起作用。
【问题讨论】:
-
我只是阅读带有
readlines的标题和带有read.delim的表格。对于写入,如果性能不是问题,您可以将带有write.delim的纯文本文件写入tempdir(),然后在该文件上执行readlines,然后使用c(headerlines, bodylines)获得组合结果。我的数据很小:) -
谢谢!我现在读入了标头和数据,但
c(header, datafile)无法正常工作。当我写出文本文件时,所有标题行都合并到数据行中并移动了所有内容。 -
你的
header和datafile的内容是什么?您是否还可以显示您希望最终预期输出如何查找此文本文件? -
我已将编辑后的“数据文件”保存在 .csv 文件中。我必须在数据中添加一些新行,并在 '''ecode''' 下填写一些变量以及您在上面的原始文本文件示例中看到的其他一些列(总共 2240 行数据,不包括列名和不可编辑的标题)。我现在需要找到一种方法来自动将这个编辑过的 .csv 读回一个文本文件,该文本文件在数据来源的文本文件中具有大的“不可编辑的标题”,这对于每个主题都是不同的(它将是相同的行数,但 ID/日期/时间都是唯一的)。
标签: r data-manipulation readlines eeglab