Skip to content

Instantly share code, notes, and snippets.

@alstat
Last active March 20, 2019 13:56
Show Gist options
  • Save alstat/11ade13dac59bc2bba9d35a312038f48 to your computer and use it in GitHub Desktop.
Save alstat/11ade13dac59bc2bba9d35a312038f48 to your computer and use it in GitHub Desktop.
set.seed(1)
x1 <- rnorm(10)
x2 <- rnorm(10)
out <- cbind(x1, x2)
colMeans(out)
# colMeans can be computed as follows as well
apply(out, 2, mean)
# apply - applies a mean function to column (indicated by 2) of out matrix
# for sd change mean to sd
apply(out, 2, sd)
# this is equivalent to
c(sd(out[, 1]), sd(out[, 2]))
# for median
apply(out, 2, median)
# this is equivalent to
c(median(out[, 1]), median(out[, 2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment