2012-10-28 25 views
10

我知道的東西已經發布,但並不像我期待的那麼完整。R輸出沒有[1],如何很好地格式化?

採取任何幫助功能(即?mean),並意識到它的輸出(或至少輸出應該能夠以相同的方式生成)。

你如何獲得進入,對齊/ intendation?

例子:

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

如果這將是R(從函數調用時),你想要的東西輸出,你怎麼讓[1]消失,值排隊?

回答

16

這是相當基本的,請諮詢An Introduction to R以及

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

等等。請參閱格式化函數中的示例(數千個)。以下是我的一個包裝中的一個簡單示例:

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) 
}