It just so happens that I created a vectorized SD function the other
day. See the column and row versions below. If there are any
rows/columns with only one non-NA value, it will return NaN.
col_sd = function(x){
dimx = dim(x)
x.mean = .Internal(colMeans(x,dimx[1],dimx[2],na.rm=TRU
Hi Jorge, this does not seem to return the same thing as
p <- array(c(1:5, rep(NA, times = 3)), dim = c(5, 5, 3))
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)
Am I missing something basic here?
On We
Dear Matt,
You're absolutely right. The reason why my code gives different results is
because it calculates the standard deviation for each row/column in all the
arrays rather than for every cell. My bad.
You can easily get the results you posted running
>apply(p,1:2,sd,na.rm=TRUE)
Here is anoth
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 wrote:
>
> Dear Matt,
>
> Here are two w
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),s
5 matches
Mail list logo