Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

formatting - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("
Call:
")
    print(x$call)
    cat("
Residuals:
")
    print(x$residSum)
    cat("
")

    printCoefmat(x$coefficients, P.values=TRUE, has.Pvalue=TRUE)
    digits <- max(3, getOption("digits") - 3)
    cat("
Residual standard error: ", formatC(x$sigma, digits=digits), " on ",
        formatC(x$df), " degrees of freedom
", sep="")
    cat("Multiple R-squared: ", formatC(x$r.squared, digits=digits),
        ",Adjusted R-squared: ",formatC(x$adj.r.squared, digits=digits),
        "
", sep="")
    invisible(x)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...