Re: [R] Sapply for descriptive statistics

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Wed, 09 Mar 2011 15:31:30 -0800

Hi:

Perhaps something like this?

m <- matrix(rnorm(100, m = 10, s = 2), ncol = 5) colnames(m) <- paste('V', 1:5, sep = '')

# Summary function:
summs <- function(x) c(mean = mean(x), sd = sd(x), range = diff(range(x)))

# Apply to columns of m and transpose the result: t(apply(m, 2, summs))

For the sample I generated, the result is
> t(apply(m, 2, summs))

        mean sd range

V1 10.304586 2.330545 9.016235
V2  9.135212 1.993268 7.528364
V3  9.873094 2.155940 7.554311
V4 10.308073 2.357703 9.483222
V5 10.532111 2.378734 8.073172

HTH,
Dennis

On Wed, Mar 9, 2011 at 2:59 PM, Tomii <diogenas_at_gmail.com> wrote:

> I try to calculate descriptive statistics for one of the variables in the
> data frame, however command sapply calculates these statistics for every
> value of the variable separately. How to make it calculate range (as well
> as
> other statistics) for all column?
>
> Here are commands and results:
>
> > as1$trust
> [1] 5.957510 5.888664 6.168135 6.419472 5.668796 6.026923
> 6.456721 7.017946 5.294411
>
> [10] 7.296844 6.479167 5.009000 7.149073 5.932667 5.991000
> 5.327137 5.453230 5.650350
> [19] 5.295608 5.518337 4.875000 6.637000 5.891014 6.726055
> 10.695650 5.490983 7.290476
> [28] 5.728543 4.103689 8.421315
> > des.trust <- sapply(as1$trust, range, na.rm=TRUE)
> > des.trust
> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
> [,8] [,9] [,10]
> [1,] 5.95751 5.888664 6.168135 6.419472 5.668796 6.026923 6.456721
> 7.017946 5.294411 7.296844
> [2,] 5.95751 5.888664 6.168135 6.419472 5.668796 6.026923 6.456721
> 7.017946 5.294411 7.296844
>
> [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
> [,19] [,20] [,21]
> [1,] 6.479167 5.009 7.149073 5.932667 5.991 5.327137 5.45323 5.65035
> 5.295608 5.518337 4.875
> [2,] 6.479167 5.009 7.149073 5.932667 5.991 5.327137 5.45323 5.65035
> 5.295608 5.518337 4.875
>
> [,22] [,23] [,24] [,25] [,26] [,27] [,28]
> [,29] [,30]
> [1,] 6.637 5.891014 6.726055 10.69565 5.490983 7.290476 5.728543
> 4.103689 8.421315
> [2,] 6.637 5.891014 6.726055 10.69565 5.490983 7.290476 5.728543
> 4.103689 8.421315
>
>
> tomii <t.klepsys_at_gmail.com>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

        [[alternative HTML version deleted]]



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. Received on Wed 09 Mar 2011 - 23:35:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Mar 2011 - 23:40:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive