All, Is this typical of how people will print a summary of results? CoinTosses <- function(n) { x <- sample(c(0,1), n, replace=TRUE) y <- x y[y==0] <- "T" y[y==1] <- "H" numHeads <- sum(x) numTails <- n-sum(x) p <- numHeads/n cat(cat(y,sep=""),"\n") cat("Number of heads: ", numHeads, "\n") cat("Number of tails: ", numTails, "\n") cat("The proportion of heads is: ", p, "\n") } CoinTosses(40) Or is another technique preferred? Thanks. David -- View this message in context: http://r.789695.n4.nabble.com/Printing-a-summary-tp4639104.html Sent from the R help mailing list archive at Nabble.com.
On Fri, Aug 3, 2012 at 3:34 PM, darnold <dwarnold45 at suddenlink.net> wrote:> All, > > Is this typical of how people will print a summary of results? > > CoinTosses <- function(n) { > x <- sample(c(0,1), n, replace=TRUE) > y <- x > y[y==0] <- "T" > y[y==1] <- "H" > numHeads <- sum(x) > numTails <- n-sum(x) > p <- numHeads/n > cat(cat(y,sep=""),"\n") > cat("Number of heads: ", numHeads, "\n") > cat("Number of tails: ", numTails, "\n") > cat("The proportion of heads is: ", p, "\n") > } > > CoinTosses(40) > > Or is another technique preferred?cat() is certainly a reasonable way to print results (print might also work) -- really whatever works for you, though I might prefer your function only return the values and that those are printed automatically at the top level. Also, I'm not sure if this is what you're getting at so feel free to ignore it if I'm off base, but http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-should-I-write-summary-methods_003f Best, Michael> > Thanks. > > David > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Printing-a-summary-tp4639104.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi, I find it better to return a named list so that users can do what they want with the results rather than just looking at them, which is all your approach allows. That's not incompatible with printing the results to the screen as well: CoinTosses <- function(n, verbose=TRUE) { x <- sample(c(0,1), n, replace=TRUE) y <- x y[y==0] <- "T" y[y==1] <- "H" numHeads <- sum(x) numTails <- n-sum(x) p <- numHeads/n if(verbose) { cat(cat(y,sep=""),"\n") cat("Number of heads: ", numHeads, "\n") cat("Number of tails: ", numTails, "\n") cat("The proportion of heads is: ", p, "\n") } list(numHeads = numHeads, numTails = numTails, p = p) } Sarah On Fri, Aug 3, 2012 at 4:34 PM, darnold <dwarnold45 at suddenlink.net> wrote:> All, > > Is this typical of how people will print a summary of results? > > CoinTosses <- function(n) { > x <- sample(c(0,1), n, replace=TRUE) > y <- x > y[y==0] <- "T" > y[y==1] <- "H" > numHeads <- sum(x) > numTails <- n-sum(x) > p <- numHeads/n > cat(cat(y,sep=""),"\n") > cat("Number of heads: ", numHeads, "\n") > cat("Number of tails: ", numTails, "\n") > cat("The proportion of heads is: ", p, "\n") > } > > CoinTosses(40)> Or is another technique preferred? > > Thanks. > > David > >-- Sarah Goslee http://www.functionaldiversity.org