Dear R users, I am trying to use apply function on a 3D array (get rid of any 
loop) but wasn't successfull. Below is a toy example of what I am trying to do: 
refID_remNoCat = 1:30;
s_remNoCat = sample(1:length(refID_remNoCat),size=length(refID_remNoCat));
s.sort_remNoCat = sort(s_remNoCat,index.return=TRUE);
p_ref_remNoCat = c(5,10);
nperm = 2;
nCat = 2;
N = length(refID_remNoCat);
p=1;
max_es_rand = matrix(rep(0,nCat*nperm),ncol=nperm,nrow=nCat);
Nh = rep(0,nCat);
NR = matrix(rep(0,nCat*nperm),ncol=nperm,nrow=nCat);
es_rand = array(rep(0,nCat*N*nperm),c(nCat,nperm,N));
p_ref_ix_remNoCat_rand = 
array(rep(0,nCat*max(p_ref_remNoCat)*nperm),c(nCat,nperm,max(p_ref_remNoCat)));
Phit = array(rep(0,nCat*N*nperm),c(nCat,nperm,N));
Pmiss = array(rep(0,nCat*N*nperm),c(nCat,nperm,N));
set.seed(1);
for (z in 1:nCat){
  for (j in 1:nperm){
    Nh[z] = N-p_ref_remNoCat[z];
    p_ref_ix_remNoCat_rand[z,j,1:p_ref_remNoCat[z]] = 
sample(1:length(refID_remNoCat),size=p_ref_remNoCat[z],replace=FALSE);
    NR[z,j] = sum(abs(s_remNoCat[p_ref_ix_remNoCat_rand[z,j,]])^p);
    for (i in 1:length(refID_remNoCat)){
      Phit[z,j,i] = 
sum(abs(s.sort_remNoCat$x[which(s.sort_remNoCat$ix[1:i]%in%p_ref_ix_remNoCat_rand[z,j,])])/NR[z,j]);
      Pmiss[z,j,i] = 
(1/(N-Nh[z]))*length(which(!s.sort_remNoCat$ix[1:i]%in%p_ref_ix_remNoCat_rand[z,j,]));
      es_rand[z,j,i]=Phit[z,j,i]-Pmiss[z,j,i];
  }
  max_es_rand[z,j]=max(es_rand[z,j,]);
  }
} The main problem is the second to last line, calculation of Phit, where I am 
summing the s.sort_remNoCat$x that are in p_ref_ix_remNoCat_rand so far, 
divided by NR. Thank you all for your help and sorry for the long variable 
names. Lin                                     
        [[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