I agree, sorting is yet another topic that is very confusing in R that dplyr makes simple with its arrange() function.

Cheers,

Bob

It just keeps getting better, thanks!

Bob

]]>mean.n <- function(x, n = 2, digits = 2) {

mns <- apply(x, 2, mean, na.rm = TRUE)

nv <- apply(x, 2, function(x) sum(!is.na(x)))

mnz = n, mns, NA)

cat(“Mean: “,sprintf(paste0(“%9.”,digits,”f”),mnz),

“\n N: “,sprintf(“%9s”,nv),”\n”)

}

I really like that approach. One of the things that surprised me the most about R is that the data argument is not supported by all functions. I see mosaic also adds formulas and the data argument to mean, sd, etc. Nice! Too bad they didn’t make na.rm = TRUE the default. Having it set to FALSE by default makes R deal with missing values the reverse of all other stat packages I know.

Cheers,

Bob

> require(mosaic)

> favstats(~ q1, data=df)

min Q1 median Q3 max mean sd n missing

1 1 1 1 1 1 0 3 0

> favstats(~ q2, data=df)

min Q1 median Q3 max mean sd n missing

2 2 2 2 2 2 0 2 1

> favstats(~ q3, data=df)

min Q1 median Q3 max mean sd n missing

3 3 3 3 3 3 NA 1 2

It also supports calculations such as:

favstats(y ~ x, data=df)

]]>I thought as.matrix() was redundant and I even read the help file which states in the Details section, “If X is not an array but an object of a class with a non-null dim value (such as a data frame), apply attempts to coerce it to an array via as.matrix if it is two-dimensional (e.g., a data frame)…” However, when I tested it, it appeared not to work without it. It retracing my steps I found the bug in the code that was the real culprit, so you’re quite right, it’s redundant. It may save a nanosecond of compute time by saving the test to see if it’s an array or not, but that’s hardly worth wasting code on.

Cheers,

Bob

nice function, I always wanted to write a similar small function because I every now and then have the need to calculate row means if at least x values are valid…

According to your example: you don’t need to coerce to matrix in the apply function, and I would not use “df” inside the function parameter of the apply-function, since it’s misleading (the parameter is the looped row, not the initial df). But this is just “cosmetics”… ]]>

funN <- function(n, fun, …) {

function(x) {

nvalid <- sum(!is.na(x))

out = n) out else NA

}

}

Cheers

Tim