11

The file I am trying to read in has a 'numeral sign-#' (aka hash symbol) in the column header. When I used read.table to load the data the columns were shifted and the column headers AFTER the hash symbol (or numeral sign-#) were missing!

How do I read in 'numeral signs' as part of my column headers,

Ex. title, author, criterion#, date, country of origin

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
mccurcio
  • 1,294
  • 5
  • 25
  • 44

1 Answers1

24

There is an argument to read.table that allows the comment character be changed:

read.table( ...., comment.char="", ...)    # or suppressed as I show here:

read.table(textConnection("title, author, criterion#, date, country of origin\nA, b, C, 1/1/1939, USA"), 
           sep=",", comment.char="", header=TRUE)
#  title author criterion.      date country.of.origin
# 1     A      b          C  1/1/1939               USA

The hash or octothorpe gets turned into a period by the check.names function which read.table calls only on line 1 if header=TRUE. (And even that coercion can be suppressed if absolutely necessary.) This question was answered before the arrival of the text="..." parameter for scan and read.table and read.-cousins, so textConnection is no longer needed for example constructions unless you use readLines. Can use read.table(text= ..<und-so-weiter>.. )

IRTFM
  • 258,963
  • 21
  • 364
  • 487