fill - R read.table columns shifted to the left -
i created file called test2.txt following information:
col1 col2 col3 col4 1 b 2 b 3 b 4 b 5 b 6 b 7 c 8 c
when reading following command:
test.ws=read.table(paste(inputdir,'test2.txt',sep=''),fill=t,header=t)
i following:
col1 col2 col3 col4 1 1 b na 2 2 b na 3 3 b na 4 4 b na 5 5 b na 6 6 b na 7 7 c na 8 8 c na
the columns shifted left. gives?!
i tried following:
> count.fields(paste(inputdir,'test.txt',sep='')) [1] 4 3 3 3 3 3 3 4 4
and it's telling me number of tabs different, isn't! information? it's worth mention that, when importing .txt file excel, reads tabs correctly , doesn't skip or shift columns.
i tried assigning column names separately, didn't work:
colnames=names(test.ws) test.ws=read.table(paste(inputdir,'test2.txt',sep=''),skip=1,fill=t,header=t,col.names=colnames)
yields:
warning message: in read.table(paste(inputdir, "test2.txt", sep = ""), skip = 1, : header , 'col.names' of different lengths
i found similar issue online: https://stat.ethz.ch/pipermail/r-help/2008-july/166676.html. question wasn't answered.
if tab separated data, set separator accordingly: sep="\t"
. otherwise (from on read.table
)
if
sep = ""
(the default read.table) separator ‘white space’, 1 or more spaces, tabs, newlines or carriage returns.
and multiple consecutive tabs being treated single delimiter.
or use read.delim
instead of read.table
has defaults better suited tab-separated data.
Comments
Post a Comment