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

From: <martin_at_martincmartin.com>
Date: Wed, 20 Aug 2008 22:45:10 +0200 (CEST)


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:

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 Received on Wed 20 Aug 2008 - 21:26:09 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 - 09:37:05 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