Hi:
Here are a few ways you could go about it using plyr functions.
null.output <- data.frame(richness = factor(rep(2:8, 4)),
metric = rnorm(28, 10, 1),
metric2 = rnorm(28, 1, 1),
metric3 = rnorm(7.5, 1, 1))
##### Method 1: Use numcolwise().
# Function to compute summaries from a variable v
f <- function(v) c(iterations = length(v),
average = mean(v, na.rm = TRUE),
lower = quantile(v, 0.025, na.rm = TRUE),
upper = quantile(v, 0.975, na.rm = TRUE))
# Apply f to each numeric variable in null.output, by richness
u <- ddply(null.output, .(richness), numcolwise(f))
# Names didn't get attached, so put these into each set of four rows
summnames <- c("iterations", "average", "lower", "upper")
u$summary <- summnames
head(u)
##### Method 2: Write a function that returns all of the summaries
##### for each input variable as a vector.
summfun <- function(d)
{
# Create the output variable names
vars <- names(d)[grepl("metric", names(d))]
snames <- c("iterations", "average", "lower", "upper")
vnames <- paste(rep(vars, each = length(summnames)),
rep(snames, length(vars)), sep = ".")
# Function to be applied to each variable
g <- function(x) c(length(x), mean(x, na.rm = TRUE),
quantile(x, c(0.025, 0.975), na.rm = TRUE))
# Convert the numeric part of the data frame to matrix
# and then apply the function g to each column
L <- apply(data.matrix(d[, vars]), 2, g)
# String the matrix in column order to a vector, attach
# the names to it, then return
v <- as.vector(L)
names(v) <- vnames
v
}
w <- ddply(null.output, .(richness), summfun)
# Another way to display the output:
library(reshape2)
dm <- melt(w, id = "richness")
dcast(dm, variable ~ richness, value.var = "value")
Hopefully one of these appeals to you...
Dennis
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
manipulatr+...@googlegroups.com.
> To post to this group, send email to
manip...@googlegroups.com.
> Visit this group at
http://groups.google.com/group/manipulatr?hl=en.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>