[R] read.table with missing data and consecutive delimiters

Tim Victor statsdoc at gmail.com
Wed Feb 11 15:53:16 CET 2015


All,

Assume we have data in an ASCII file that looks like

Var1$Var2$Var3$Var4
1$2$3$4
2$$5
$$$6

When I execute

read.table( 'test.dat', header=TRUE, sep='$' )

I, of course, receive the following error:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
 :
  line 2 did not have 4 elements

When I set fill=TRUE, e.g., read.table( 'test.dat', header=TRUE, sep='$',
fill=TRUE )

I get:

  Var1 Var2 Var3 Var4
1    1    2    3    4
2    2   NA    5   NA
3   NA   NA   NA    6


What I need is

  Var1 Var2 Var3 Var4
1    1    2    3    4
2    2   NA   NA    5
3   NA   NA   NA    6

What am I missing?

Thanks,

Tim

	[[alternative HTML version deleted]]



More information about the R-help mailing list