Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can read 'Numeral Signs-#' as part of a column header?

Tags:

r

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

like image 306
mccurcio Avatar asked Aug 17 '11 01:08

mccurcio


1 Answers

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>.. )

like image 185
IRTFM Avatar answered Sep 30 '22 06:09

IRTFM