【问题标题】:readlines() and writelines() with read.delim带有 read.delim 的 readlines() 和 writelines()
【发布时间】: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) 无法正常工作。当我写出文本文件时,所有标题行都合并到数据行中并移动了所有内容。
  • 你的headerdatafile的内容是什么?您是否还可以显示您希望最终预期输出如何查找此文本文件?
  • 我已将编辑后的“数据文件”保存在 .csv 文件中。我必须在数据中添加一些新行,并在 '''ecode''' 下填写一些变量以及您在上面的原始文本文件示例中看到的其他一些列(总共 2240 行数据,不包括列名和不可编辑的标题)。我现在需要找到一种方法来自动将这个编辑过的 .csv 读回一个文本文件,该文本文件在数据来源的文本文件中具有大的“不可编辑的标题”,这对于每个主题都是不同的(它将是相同的行数,但 ID/日期/时间都是唯一的)。

标签: r data-manipulation readlines eeglab


【解决方案1】:

查看我的代码。应该很快。

library(tidyverse)
library(data.table)
library(fs)

dataRead = function(file) fread(
  file = file, skip=26, 
  col.names = c("item","bepoch","ecode","label","onset","diff",
                "dura","b_flags","a_flags","enable","bin","bin2"),
  colClasses = c("integer", "integer", "integer", "character",
                 "double", "double", "double", "character",
                 "character", "integer", "character", "character")) %>% 
  as_tibble() %>% 
  mutate(bin = str_c(bin, "    ", bin2)) %>% select(-bin2)
  
width = c(1, 5, 9, 10, 11, 9, 6, 11, 11, 5, 8)
files = dir_ls("txtfiles", regexp = "\\.txt$")
if(length(files)>0){
  for(i in 1:length(files)){
    header = fread(file = files[i], nrows=24, sep = "|", header=FALSE)
    df = dataRead(files[i])
    df = df %>% mutate(bin = "[xxxx]")
    df = df %>% mutate(across(everything(), 
                              ~str_pad(.x, width[which(names(df)==cur_column())])))
    fwrite(header, files[i], append = FALSE, quote = FALSE, col.names = FALSE)
    fwrite(df, files[i], append = TRUE, col.names = FALSE, sep = " ", quote = FALSE)
  }
}

程序处理 txtfiles 文件夹中的每个 txt 文件。将标头和数据读入tibble,改变tibble,然后写回文本文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多