Hi, I deal with 3D array say:
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 1 4 7 10 13
[2,] 2 5 8 11 14
[3,] 3 6 9 12 15
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 16 19 22 25 28
[2,] 17 20 23 26 29
[3,] 18 21 24 27 30
, , 3
[,1] [,2] [,3] [,4] [,5]
[1,] 31 34 37 40 43
[2,] 32 35 38 41 44
[3,] 33 36 39 42 45
I want to calculate means in the cells with respect to dimention 3rd so for
example (1+16+31)/3 or (15+30+45)/3 AND avoid taking zeros into account.
I did it with a loop but I guess there is the other more straightforward
method. will be very grateful for help...
best, rob
--
View this message in context:
http://www.nabble.com/3D-array-tf4940356.html#a14142187
Sent from the R help mailing list archive at Nabble.com.
say your array is called "no.named.array". then: no.named.array[no.named.array == 0] <- NA rowMeans(no.named.array, dims=2, na.rm=T) b On Dec 3, 2007, at 7:24 PM, threshold wrote:> > Hi, I deal with 3D array say: > > , , 1 > > [,1] [,2] [,3] [,4] [,5] > [1,] 1 4 7 10 13 > [2,] 2 5 8 11 14 > [3,] 3 6 9 12 15 > > , , 2 > > [,1] [,2] [,3] [,4] [,5] > [1,] 16 19 22 25 28 > [2,] 17 20 23 26 29 > [3,] 18 21 24 27 30 > > , , 3 > > [,1] [,2] [,3] [,4] [,5] > [1,] 31 34 37 40 43 > [2,] 32 35 38 41 44 > [3,] 33 36 39 42 45 > > I want to calculate means in the cells with respect to dimention 3rd > so for > example (1+16+31)/3 or (15+30+45)/3 AND avoid taking zeros into > account. > > I did it with a loop but I guess there is the other more > straightforward > method. will be very grateful for help... > > best, rob > -- > View this message in context: http://www.nabble.com/3D-array-tf4940356.html#a14142187 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
The following will work:
apply(my.array, c(1,2), mean)
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of threshold
Sent: Monday, December 03, 2007 7:25 PM
To: r-help at r-project.org
Subject: [R] 3D array
Hi, I deal with 3D array say:
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 1 4 7 10 13
[2,] 2 5 8 11 14
[3,] 3 6 9 12 15
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 16 19 22 25 28
[2,] 17 20 23 26 29
[3,] 18 21 24 27 30
, , 3
[,1] [,2] [,3] [,4] [,5]
[1,] 31 34 37 40 43
[2,] 32 35 38 41 44
[3,] 33 36 39 42 45
I want to calculate means in the cells with respect to dimention 3rd so for
example (1+16+31)/3 or (15+30+45)/3 AND avoid taking zeros into account.
I did it with a loop but I guess there is the other more straightforward
method. will be very grateful for help...
best, rob
--
View this message in context:
http://www.nabble.com/3D-array-tf4940356.html#a14142187
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
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.