Re: some questions around statistical measures
Hi,
I will try to answer all your questions at once. You want to try to avoid using loops wherever possible. In this case, the apply() function should work for you. The format is
apply(x, margin, function)
where x is the data frame or matrix, margin is 1 for rows, 2 for columns, and function is any function you want (built in or written by you).
Som apply(x, 1, median) will get the row medians of x.
Here is an example. Give it a try.
options(digits=3)
# create some data
x < matrix(rnorm(15, 10, 2), nrow=3)
x < as.data.frame(x)
x
# write a function to create some statistics
mystats < function(x, na.omit=TRUE){
if (na.omit)
x < x[!is.na(x)]
m < mean(x)
md < median(x)
min < min(x)
max < max(x)
return(c(mean=m, median=md, min=min, max=max))
}
# apply the function to the rows of the data frame
results < apply(x, 1, mystats)
results
# transpose the results
t(results)
