read.table()报错 incomplete final line; line xx did not have xx elements

问题

今天在读取一个.xls文件的时候报错,incomplete final line。

> dat <- read.table("pca_pop.xls")
Warning message:
In read.table("pca_pop.xls") :
  incomplete final line found by readTableHeader on 'pca_pop.xls'

.xls文件另存为.txt,继续报错,line 2 did not have 6 elements。

> dat2 <- read.table("pca_pop.txt")
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : 
  line 2 did not have 6 elements

解决办法

1.另存为.csv

> popinfo <- read.csv("pca_pop.csv")
> head(popinfo)
  order exp_id vcf_id group   color pch
1     1      1      1    G2 #9ACD32  16
2     2      2      2    G2 #9ACD32  16
3     3      3      3    G2 #9ACD32  16
4     4      4      4    G2 #9ACD32  16
5     5      5      5    G2 #9ACD32  16
6     6      6      6    G2 #9ACD32  16

2.由于数据文件中存在#,被视为注释内容,所以添加comment.char = ""

> test <-read.table("pca_pop.txt",comment.char = "",header = T)
> head(test)
  order exp_id vcf_id group   color pch
1     1      1      1    G2 #9ACD32  16
2     2      2      2    G2 #9ACD32  16
3     3      3      3    G2 #9ACD32  16
4     4      4      4    G2 #9ACD32  16
5     5      5      5    G2 #9ACD32  16
6     6      6      6    G2 #9ACD32  16

如有错误敬请指出,引用请注明出处

你可能感兴趣的:(read.table()报错 incomplete final line; line xx did not have xx elements)