R - 将 <.out> 转换为 <.csv> 格式

R - convert <.out> into <.csv> format

我需要将模型输出文件转换为 .csv 格式。原始文件是 .out 格式,是数据表,中间有几个空格分隔:

例如原始文件大约有 14,000 行,但结构相同。 请使用此 link 作为示例文件: https://www.dropbox.com/sh/1w0f9cq2w6gcbjo/AACoGu6L3yKBGBKODY87lygsa?dl=0

time   dayofyr   nit_N2O-N  dnit_N2O-N   dnit_N2-N        NO-N  CUM-N2O(gN/ha)  CUM-NO(gN/ha)
1980.00     1      0.3310        0.3403        0.2022        0.0000        0.6713        0.0000  
1980.00     2      0.3295        0.3400        0.2020        0.0000        1.3408        0.0000  
1980.00     3      0.3280        0.3397        0.2018        0.0000        2.0086        0.0000  
1980.00     4      0.3265        0.3395        0.2016        0.0000        2.6746        0.0000  
1980.00     5      0.3251        0.3393        0.2015        0.0000        3.3389        0.0000  
1980.00     6      0.3237        0.3391        0.2014        0.0000        4.0017        0.0000  
1980.00     7      0.3223        0.3389        0.2013        0.0000        4.6629        0.0000  
1980.00     8      0.3209        0.3387        0.2011        0.0000        5.3225        0.0000  
1980.00     9      0.3195        0.3386        0.2010        0.0000        5.9805        0.0000  
1980.00    10      0.3198        0.4589        0.2868        0.0000        6.7592        0.0000  

我已经尝试将扩展名更改为 .csv 并将连续的空格转换为“,”,但恐怕在正则表达式语法或整体原理中我遗漏了一些东西。

这就是我到目前为止所得到的:

# change extension of all .out files:
outFiles <- dir(cntPath, "^.+\.out", full.names = TRUE, ignore.case = TRUE, all.files = TRUE)
for (nOut in outFiles){
  newOut <- gsub('.out', '.csv', outFiles) # rename files to avoid accidental re-processing
  file.rename(outFiles, newOut)
}
# for each file, eliminate initial spaces, and replace consecutive spaces with a comma, for all rows and columns.
t1 <- read.csv(paste(cntPath, "test2.csv", sep = "/"), header = TRUE, sep = "\t", blank.lines.skip = FALSE)
for (i in 1:length(nrow(t1))) {
  sub("^\s+", "^\S", i)
  sub("\s+", ",", i)
}

应该比较容易,但是我不知道哪里出错了。 任何 help/suggestions 都会受到赞赏。 谢谢

首先,把你所有的.out文件备份到某个地方,这样你就可以在出现问题时恢复它们。

备份文件后,在R中进行以下操作。

下面的代码块将在fp指定的目录中找到.out个文件,读取它们,将它们写入.csv最后删除所有.out个文件.如果不想删除文件,最后一行注释为unlink.

编辑: 使用 tryCatch 块来查找读|写失败的文件

fp <- "."  # specify file path
fl <- list.files( path = fp, pattern = "*.out", full.names = TRUE ) # get .out files
for (file in fl ){  # loop through files, read it and write it as .csv files. Then delete .out files
flag <- tryCatch( {
    write.table( x = read.table(file = file, header = TRUE ), 
                 file = file.path( fp, gsub( "out$", "csv", basename( file ) ) ), 
                 sep = ",", 
                 row.names = FALSE )
    TRUE
    },
    error = function( x ) { 
      print( paste0( "Problem reading and writing file : ", file ) )
      return(FALSE) } )

if( flag ) unlink( file )  # deletes .out files
}

您的 .out 文件似乎是 fixed-width 格式,但存在名称不对齐的小问题。

没问题,跳过那一行

library(readr)

fwf_txt <- "time   dayofyr   nit_N2O-N  dnit_N2O-N   dnit_N2-N        NO-N  CUM-N2O(gN/ha)  CUM-NO(gN/ha)
1980.00     1      0.3310        0.3403        0.2022        0.0000        0.6713        0.0000  
1980.00     2      0.3295        0.3400        0.2020        0.0000        1.3408        0.0000  
1980.00     3      0.3280        0.3397        0.2018        0.0000        2.0086        0.0000  
1980.00     4      0.3265        0.3395        0.2016        0.0000        2.6746        0.0000  
1980.00     5      0.3251        0.3393        0.2015        0.0000        3.3389        0.0000  
1980.00     6      0.3237        0.3391        0.2014        0.0000        4.0017        0.0000  
1980.00     7      0.3223        0.3389        0.2013        0.0000        4.6629        0.0000  
1980.00     8      0.3209        0.3387        0.2011        0.0000        5.3225        0.0000  
1980.00     9      0.3195        0.3386        0.2010        0.0000        5.9805        0.0000  
1980.00    10      0.3198        0.4589        0.2868        0.0000        6.7592        0.0000  "

write_csv(read_fwf(fwf_txt, fwf_empty(fwf_txt, skip = 1),
                   skip = 1),
          path = "fwf.csv",
          col_names = FALSE)

reprex package (v0.2.0) 创建于 2018-03-25。