Hi Mark,
There is a typo in the first way. My apologies. It should be:
# First
res<-apply(p,3,function(X)
       c(scols=apply(X,2,sd,na.rm=TRUE),srows=apply(X,1,sd,na.rm=TRUE))
       )
res

HTH,

Jorge


On Wed, Feb 25, 2009 at 11:42 AM, Jorge Ivan Velez <jorgeivanve...@gmail.com
> wrote:

>
> Dear Matt,
>
> Here are two ways:
>
> # Data
> p <- array(c(1:5, rep(NA, times = 3)), dim = c(5, 5, 3))
>
> # First
> res<-apply(p,3,function(X)
>        c(scols=apply(X,2,sd,na.rm=TRUE),srows=apply(X,2,sd,na.rm=TRUE))
>        )
> res
>
> # Second
> res2<-apply(p,3,function(X)
>        list(scols=apply(X,2,sd,na.rm=TRUE),srows=apply(X,1,sd,na.rm=TRUE))
>        )
>
> lapply(res2,function(x) do.call(rbind,x))
>
> HTH,
>
> Jorge
>
>
> On Wed, Feb 25, 2009 at 10:53 AM, Matt Oliver <moli...@udel.edu> wrote:
>
>> Dear help, suppose I have this array and want to compute sd aross rows and
>> columns.
>>
>> p <- array(c(1:5, rep(NA, times = 3)), dim = c(5, 5, 3))
>>
>> apply(p, 1:2, sd) fails because sd requires at least 2 numbers to compute
>> sd
>>
>> apply(p, 1:2, sd, na.rm = TRUE) fails for the same reason
>>
>> I crafted my own function that does what I want
>>
>> sd_fun <- function(i){
>> if(sum(!is.na(i))==0){
>> temp.sd <- NA
>> }else{
>> temp.sd <- sd(i, na.rm = TRUE)
>> }
>> return(temp.sd)
>> }
>>
>>
>> apply(p, 1:2, sd_fun)
>>
>> This does what I want, but when I scale up to large arrays like
>>
>> pp <- array(c(1:5, rep(NA, times = 3)), dim = c(1000, 1000, 60))
>>
>> the apply function takes a long time to run.
>>
>> Is there a faster, more efficient way to do this?
>>
>> Thanks in advance
>>
>> Matt
>>
>>
>> --
>> Matthew J. Oliver
>> Assistant Professor
>> College of Marine and Earth Studies
>> University of Delaware
>> 700 Pilottown Rd.
>> Lewes, DE, 19958
>> 302-645-4079
>> http://www.ocean.udel.edu/people/profile.aspx?moliver
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@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@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.

Reply via email to