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

前端 未结 1 975
庸人自扰
庸人自扰 2020-12-16 01:23

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 co

相关标签:
1条回答
  • 2020-12-16 01:53

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

    0 讨论(0)
提交回复
热议问题