R读取数据出现“line 1 appears to contain embedded nulls”的解决方法

由于数据可能在Windows下编辑过,保存的是UTF-16的格式用R读取可能会出现以下问题。这种情况有以下三种解决方案。

> sampInfo=read.table("/media/xxx/sampInfo_origin.txt", na.strings=c("", "NA"), sep="\t", header=T)
Error in make.names(col.names, unique = TRUE) : 
  invalid multibyte string at 'R'
In addition: Warning messages:
1: In read.table("/media/xxx/sampInfo_origin.txt",  :
  line 1 appears to contain embedded nulls
2: In read.table("/media/xxx/sampInfo_origin.txt",  :
  line 2 appears to contain embedded nulls
3: In read.table("/media/xxx/sampInfo_origin.txt",  :
  line 3 appears to contain embedded nulls
4: In read.table("/media/xxx/sampInfo_origin.txt",  :
  line 4 appears to contain embedded nulls
5: In read.table("/media/albert/xxx/sampInfo_origin.txt",  :
  line 5 appears to contain embedded nulls

解决方法一:fileEncoding="UTF16LE"或者fileEncoding="UTF16"

> sampInfo=read.table("/media/xxx/sampInfo_origin.txt", fileEncoding="UTF16LE", sep="\t", header=T)
> sampInfo=read.table("/media/xxx/sampInfo_origin.txt", fileEncoding="UTF16", sep="\t", header=T)
> head(sampInfo)
         Run Sample_Name age ancestry arthropathymeds biologics das_score
1 SRRxxx72  GSMxxx25  66                               NA
2 SRRxxx73  GSMxxx26  72                               NA
3 SRRxxx75  GSMxxx28  61                               NA
4 SRRxxx74  GSMxxx27  72                               NA
5 SRRxxx76  GSMxxx29  50                               NA
6 SRRxxx77  GSMxxx30  59                               NA
  disease_activity donor gender leflumide nsaids othermeds phenotype
1               C137   male                  Healthy
2               C141   male                  Healthy
3               C383   male                  Healthy
4               C148 female                  Healthy
5               C391 female                  Healthy
6               C392 female                  Healthy
  classification status plaquenil rituximab steroids sulfasalazine tissue
1              H      H                             Blood
2              H      H                             Blood
3              H      H                             Blood
4              H      H                             Blood
5              H      H                             Blood
6              H      H                             Blood

解决方法二:在Excel中打开,另存为csv文件即可。

> sampInfo=read.csv("/media/xxx/sampInfo_origin.csv", comment.char = "#", sep=",", header=T)
> head(sampInfo)
         Run Sample_Name age ancestry arthropathymeds biologics das_score
1 SRRxxx72  GSMxxx25  66                               NA
2 SRRxxx73  GSMxxx26  72                               NA
3 SRRxxx75  GSMxxx28  61                               NA
4 SRRxxx74  GSMxxx27  72                               NA
5 SRRxxx76  GSMxxx29  50                               NA
6 SRRxxx77  GSMxxx30  59                               NA
  disease_activity donor gender leflumide nsaids othermeds phenotype
1               C137   male                  Healthy
2               C141   male                  Healthy
3               C383   male                  Healthy
4               C148 female                  Healthy
5               C391 female                  Healthy
6               C392 female                  Healthy
  classification status plaquenil rituximab steroids sulfasalazine tissue
1              H      H                             Blood
2              H      H                             Blood
3              H      H                             Blood
4              H      H                             Blood
5              H      H                             Blood
6              H      H                             Blood

解决方法三:在linux系统里将sampInfo_origin.txt用gedit打开,另存为sampInfo_origin01.txt,“Character Encoding” 改为 UTF-8, “Line ending”改为“Unix/Linux”。

> sampInfo=read.table("/media/xxx/sampInfo_origin01.txt", sep="\t", header=T)
> head(sampInfo,2)
         Run Sample_Name age ancestry arthropathymeds biologics das_score
1 SRRxxx72  GSMxxx25  66                               NA
2 SRRxxx73  GSMxxx26  72                               NA
  disease_activity donor gender leflumide nsaids othermeds phenotype
1               C137   male                  Healthy
2               C141   male                  Healthy
  classification status plaquenil rituximab steroids sulfasalazine tissue
1              H      H                             Blood
2              H      H                             Blood

你可能感兴趣的:(R读取数据出现“line 1 appears to contain embedded nulls”的解决方法)