R read.table 跳过参数跨平台不一致

问题描述

我在两个不同的平台上运行 R:

Windows 上的一个

> version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64,mingw32             
status                                     
major          4                           
minor          0.2                         
year           2020                        
month          06                          
day            22                          
svn rev        78730                       
language       R                           
version.string R version 4.0.2 (2020-06-22)
nickname       Taking Off Again    

Ubuntu/Linux 上的另一个

> version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64,linux-gnu           
status                                     
major          4                           
minor          0.4                         
year           2021                        
month          02                          
day            15                          
svn rev        80002                       
language       R                           
version.string R version 4.0.4 (2021-02-15)
nickname       Lost Library Book 

令我非常沮丧的是,我发现使用相同的 read.table 参数运行 skip 会在两个系统上返回不同的结果。

当我在 Windows 上运行以下命令 read.table("data2.txt",skip = 6,fill = F,sep = "\t") 时:

> read.table("data2.txt",sep = "\t")
   V1                                     V2 V3 V4
1  NA                             Aerococcus  3  3
2  NA             Enterobacter / Citrobacter  4  4
3  NA                  Enterococcus faecalis  3  3
4  NA                   Enterococcus faecium  4  4
5  NA                       Escherichia coli 35 35
6  NA                             Klebsiella  7  7
7  NA Morganella / Providencia / P. vulgaris  1  1
8  NA                      Proteus mirabilis 10 10
9  NA                 Pseudomonas aeruginosa 10 10
10 NA                               Serratia  2  2
11 NA                  Staphylococcus aureus  3  3

在 Linux 上运行相同的命令,我得到:

> read.table("data2.txt",sep = "\t")
   V1                                     V2     V3   V4
1  NA             Erreger-Gruppe / Intervall Gesamt 2019
2  NA                                 Gesamt     82   82
3  NA                             Aerococcus      3    3
4  NA             Enterobacter / Citrobacter      4    4
5  NA                  Enterococcus faecalis      3    3
6  NA                   Enterococcus faecium      4    4
7  NA                       Escherichia coli     35   35
8  NA                             Klebsiella      7    7
9  NA Morganella / Providencia / P. vulgaris      1    1
10 NA                      Proteus mirabilis     10   10
11 NA                 Pseudomonas aeruginosa     10   10
12 NA                               Serratia      2    2
13 NA                  Staphylococcus aureus      3    3

您可以从 dropbox 下载 data2.txt

有没有办法让两个命令的行为保持一致?不幸的是,我不知道如何更正 read.table

中的行尾

谢谢。

解决方法

它包含奇怪的换行符。例如在 mg 中打开它:

enter image description here

如果我在第 3 行删除多余的回车符,并使用 skip=4 读取它,似乎没问题:

Linux:


> read.table("data2.txt",skip = 4,header=T,fill = F,sep = "\t")
    X             Erreger.Gruppe...Intervall Gesamt X2019
1  NA                                 Gesamt     82    82
2  NA                             Aerococcus      3     3
3  NA             Enterobacter / Citrobacter      4     4
4  NA                  Enterococcus faecalis      3     3
5  NA                   Enterococcus faecium      4     4
6  NA                       Escherichia coli     35    35
7  NA                             Klebsiella      7     7
8  NA Morganella / Providencia / P. vulgaris      1     1
9  NA                      Proteus mirabilis     10    10
10 NA                 Pseudomonas aeruginosa     10    10
11 NA                               Serratia      2     2
12 NA                  Staphylococcus aureus      3     3

窗口:

enter image description here

如果你再深入一步,逐个字节地查看文件,你会发现它确实连续有两个回车符(0D)(它不应该有),而不是预期的序列 0D0A 作为换行符,因为 windows 喜欢它们):

enter image description here

更好的方法

如果您使用 data.table::fread,无论回车错位如何,它似乎都能做正确的事情:

dim( data.table::fread("data2.txt") ) 在 linux 和 windows 上都给出 12 4