Displaying 1 result from an estimated 1 matches for "sd_fun".
2009 Feb 25
1
Computing sd across an array with missing values
...rray 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 t...