Re: [Rd] Improvements to write.arff (PR#12574)

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu, 21 Aug 2008 09:36:48 +0100 (BST)

Thank you, but please see the R FAQ and report facts. We need examples of what goes wrong, and we will also need to document the fixes (e.g. names get mangled).

We do prefer patches against the actual sources (lots of comments have been missed in your version): the current source tarball for foreign is on CRAN. On Wed, 20 Aug 2008, martin_at_martincmartin.com wrote:

> Full_Name: Martin C. Martin
> Version: 2.7.1
> OS: Ubuntu
> Submission from: (NULL) (75.150.115.86)
>
>
> The function write.arff, in the foreign library:
>
> - Can produce relation names with invalid characters
> - Doesn't use colnames() for attribute names when writing a matrix.
>
> Here's a better version:
>
> write.arff <- function (x, file, eol = "\n")
> {
> if (file == "")
> file <- stdout()
> else if (is.character(file)) {
> file <- file(file, "w")
> on.exit(close(file))
> }
> if (!inherits(file, "connection"))
> stop("Argument 'file' must be a character string or connection.")
> if (!is.data.frame(x) && !is.matrix(x))
> x <- data.frame(x)
> squote <- function(s) {
> ifelse(is.na(s), s, sprintf("'%s'", gsub("(['\\])", "\\\\\\1",
> s)))
> }
> text <- paste('@relation "', make.names(deparse(substitute(x))), '"',
> sep='')
> writeLines(text, file, sep = eol)
> for (name in colnames(x)) {
> text <- paste("@attribute", name)
> if (is.data.frame(x) && is.factor(x[[name]])) {
> lev <- squote(levels(x[[name]]))
> levels(x[[name]]) <- lev
> text <- paste(text, " {", paste(lev, collapse = ","),
> "}", sep = "")
> }
> else if (is.character(x[,name])) {
> text <- paste(text, "string")
> x[,name] <- squote((x[,name]))
> }
> else if (inherits(x[,name], "POSIXt")) {
> text <- paste(text, "date \"yyyy-MM-dd hh:mm:ss\"")
> x[,name] <- squote(format(x[,name]))
> }
> else {
> text <- paste(text, "numeric")
> }
> writeLines(text, file, sep = eol)
> }
> writeLines("@data", file)
> write.table(x, file = file, na = "?", sep = ",", eol = eol,
> quote = FALSE, row.names = FALSE, col.names = FALSE)
> }
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 21 Aug 2008 - 08:39:24 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Aug 2008 - 10:37:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive