[R] comments in scan

Duncan Murdoch murdoch at stats.uwo.ca
Tue Nov 28 01:39:01 CET 2006


On 11/27/2006 7:25 PM, Jarrett Byrnes wrote:
> I had a question about scan in R.  For better code readability, I  
> would like to have lines in the block of data to be scanned that are  
> commented - not just lines that have a comment at the end.  For example
> 
> #age, weight, height
> 33,128,65
> 34,56,155
> 
> instead of having to do something like
> 
> 33,128,65 #age, weight, height
> 34,56,155
> 
> 
> Is this at all possible?

If it's on the first line, it's easy:  just use skip=1 to skip the first 
line.

The general case

#age, weight, height
33,128,65
# and now for something completely different
34,56,155

probably needs something like this:

  scan("foo", what=list(0,0,0), comment.char="#", sep=",", multi.line=T)

i.e. you need to tell it how many objects are in a record, and allow 
records to span multiple lines.  Watch out for typos in the file that 
put different numbers of entries on each line, because scan() won't 
complain about it.

Duncan Murdoch



More information about the R-help mailing list