Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R output without [1], how to nicely format?

I know stuff has been posted, but not as complete as what I am looking for.

Take any help function (i.e. ?mean), and realise that it's output (or at least output should be able to be generated in the same manner).

How do you get enters, alignment/intendation?

Example:

strings <- c("t", "df", "p-value", "mean of x", "mean of y")
values  <- c(t, df, pvalue, mean1, mean2)

If this would be the things you'd want to output in R (when called from a function), how do you make the [1] disappear, and the values lined up?

like image 841
PascalVKooten Avatar asked Oct 28 '12 14:10

PascalVKooten


People also ask

What does format function do in R?

format() function in R Programming Language is used to format strings and numbers in a specified style.

How do I print numbers in R?

Print values on R console or file using cat() or print()function can be used to print the argument. Print also returns the argument so it can be assigned. The “digits” argument specify the number of digits that should be displayed. Numbers are rounded off to that digits.

How do I print text in R?

To display ( or print) a text with R, use either the R-command cat() or print(). Note that in each case, the text is considered by R as a script, so it should be in quotes. Note there is subtle difference between the two commands so type on your prompt help(cat) and help(print) to see the difference.


1 Answers

This is rather elementary, please consult An Introduction to R as well as

  • help(cat)
  • help(sprintf)
  • help(format)

and many more. See the (literally thousands) of examples in formatting functions. Here is a simple example from one of my packages:

print.summary.fastLm <- function(x, ...) {
    cat("\nCall:\n")
    print(x$call)
    cat("\nResiduals:\n")
    print(x$residSum)
    cat("\n")

    printCoefmat(x$coefficients, P.values=TRUE, has.Pvalue=TRUE)
    digits <- max(3, getOption("digits") - 3)
    cat("\nResidual standard error: ", formatC(x$sigma, digits=digits), " on ",
        formatC(x$df), " degrees of freedom\n", sep="")
    cat("Multiple R-squared: ", formatC(x$r.squared, digits=digits),
        ",\tAdjusted R-squared: ",formatC(x$adj.r.squared, digits=digits),
        "\n", sep="")
    invisible(x)
}
like image 94
Dirk Eddelbuettel Avatar answered Sep 30 '22 19:09

Dirk Eddelbuettel