# Re: [Rd] function "apply" and 3D arrays (PR#7221)

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Fri 10 Sep 2004 - 04:33:17 EST

jaroslaw.w.tuszynski@saic.com writes:

> Full_Name: jarek tuszynski
> Version: 1.8.1
> OS: windows 2000
> Submission from: (NULL) (198.151.13.10)
>
>
> Example code:
> > a=array(1:27, c(3,3,3))
> > apply(a,2, var)
> [,1] [,2] [,3]
> [1,] 1 1 1
> [2,] 1 1 1
> [3,] 1 1 1
> [4,] 1 1 1
> [5,] 1 1 1
> [6,] 1 1 1
> [7,] 1 1 1
> [8,] 1 1 1
> [9,] 1 1 1
> > apply(a,2, mean)
> [1] 11 14 17
> > apply(a,2, sd)
> [,1] [,2] [,3]
> [1,] 1 1 1
> [2,] 1 1 1
> [3,] 1 1 1
>
> I could not figure out from the documentation how MARGIN argument of function
> "apply" works in case of arrays with dimentions larger than 2, so I created the
> above test code. I still do not know how it suppose to work but I should not get
> the results with different dimentions, while calculating var and sd.

All in accordance with documentation, i.e. no bug. The fundamental issue is that

> mean(a[,3,])

[1] 17
> var(a[,3,])

[,1] [,2] [,3]

```[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1
```

> sd(a[,3,])

[1] 1 1 1

In particular, var() gives a covariance matrix, but there's no such thing as an sd() matrix, so you get the columnwise sd().

```--
O__  ---- Peter Dalgaard             Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
(*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907

______________________________________________
R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
```
Received on Fri Sep 10 04:37:24 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:45:14 EST