[R] read.table: check.names arg - feature request
Tony Plate
tplate at blackmesacapital.com
Thu Sep 4 19:50:47 CEST 2003
If you have a special need, it probably far more time effective on your
part to write a special function rather than attempt to convince the very
busy R developers that they should add a new feature to a very old function
(is this a better use of their time than, say, fixing long identified bugs
that have not yet been fixed for lack of time?).
You should be able to do something like:
my.read.table <- function(..., header=T, comment.char="", check.names=F) {
x <- read.table(..., header=header, comment.char=comment.char,
check.names=check.names)
colnames(x)[1] <- gsub("#", "", colnames(x)[1])
x
}
And with your example on my clipboard:
> readLines("clipboard", -1)
[1] "#key value" "foo 1.2" "boo 1.3"
> read.table("clipboard", header=T, comment.char="", check.names=F)
#key value
1 foo 1.2
2 boo 1.3
> my.read.table("clipboard")
key value
1 foo 1.2
2 boo 1.3
>
hope this helps,
Tony Plate
At Thursday 10:00 AM 9/4/2003 -0700, Vadim Ogranovich wrote:
>I admit I should have been more clear in my original posting. Let me try
>again (and I do know that by deafulat read.table discards everything after
>'#' which is why I use comment.char="", my bad not to mention this).
>
>
>Here is a typical example of my data file:
>
>#key value
>foo 1.2
>boo 1.3
>
>As you see the header line begins with '#' and then lists the column
>names, however make.names will convert the raw names c("#key", "value")
>to c(".key", "value") while I need c("key", "value"), i.e. no dot before
>key. So I am asking to give us a hook to specify the function that will
>handle this situation.
>
>
>
>I am not sure I understand how having this hook can result in an invalid
>data frame? It can return invalid names, but check.names=FALSE can too.
>
>Thanks,
>Vadim
>
>-----Original Message-----
>From: Martin Maechler [mailto:maechler at stat.math.ethz.ch]
>Sent: Thursday, September 04, 2003 1:28 AM
>To: Vadim Ogranovich
>Cc: R-Help (E-mail)
>Subject: Re: [R] read.table: check.names arg - feature request
>
>
> >>>>> "Vadim" == Vadim Ogranovich <vograno at evafunds.com>
> >>>>> on Wed, 3 Sep 2003 14:29:25 -0700 writes:
>
> Vadim> Hi, I thought it would be convenient if the
> Vadim> check.names argument to read.table, which currently
> Vadim> can only be TRUE/FALSE, could take a function value
> Vadim> as well. If the function is supplied it should be
> Vadim> used instead of the default make.names.
>
>One could, but it's not necessary in your case (see below), and
>it's a potential pit to fall in.. We want read.table() to
>return valid data frames.
>
> Vadim> Here is an example where it can come in handy. I tend
> Vadim> to keep my data in coma-separated files with a header
> Vadim> line. The header line is prefixed with a comment sign
> Vadim> '#' to simplify identification of these lines. Now
> Vadim> when I read.table the files the '#' is converted to
> Vadim> '.' while I want it to be discarded.
>
>Hmm, are you using a very old version of R,
>or haven't you seen the `comment.char = "#"' argument of
>read.table()?
>
>Reading "?read.table", also note the note about
>`blank.lines.skip' , and then realize that the default for
>blank.lines.skip is ` !fill ' and that `fill = TRUE' for all
>the read.csv* and read.delim* incantation of read.table().
>
>In sum, it's very easy to use current read.table() for your
>situation!
>
> Vadim> P.S. I don't know if r-help is the right place for
> Vadim> feature requests. If it's not please let me know
> Vadim> where the right one is.
>
>Since your proposal can be interpreted as "How do I use
>read.table() when my file has comment lines?",
>r-help has been very appropriate.
>
>Otherwise, and particularly if the proposal is more technical,
>R-devel would be better suited.
>
>Regards,
>Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/
>Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27
>ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND
>phone: x-41-1-632-3408 fax: ...-1228 <><
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
More information about the R-help
mailing list