[R] apply problem

Amen Alyaari amen.alyaari at bordeaux.inra.fr
Fri Jul 12 15:20:28 CEST 2013


I will try to explain my problem. There are 600 (global map)files (1440 
sample * 720 lines)in two directories dir1 and dir2, which have the same 
format ,byte,extend,etc. I computed the `bias` between two datasets 
using the function and code given below as follows:

Function:

         bias <-function(pred,obs,na.rm=TRUE){
                      mean((pred - obs), na.rm = na.rm)}

read files:

          dir1 <- list.files("/donnees/notghi", "*.img", full.names = TRUE)
          dir2 <- list.files("/donnees/baieSt", "*.img", full.names = TRUE)

for each x,y location, I would read it into a multi-dimensional array 
with dimensions:

           file_tot<-array(dim=c(1440,720,600,2))

Apply the code:

       for(i in 1:length(dir1)){
       file_tot[,,i,1] <- readBin(dir1[i], double(), size = 4 ,n = 1440* 
720 , signed = T)
       file_tot[,,i,2] <- readBin(dir2[i], double(), size = 4 ,n = 1440 
* 720, signed = T)
      }
calculate the `bias`:

         result<-apply(file_tot,c(1,2),function(x){bias(x[,1],x[,2])})
This worked correctly and no problem with that.

now I want to modify the function and add third part(mod in the function 
which should come from different data (dir3) other than dir1 and dir2):
  function:

        err<-function(pred,obs,mod,na.rm=TRUE){
             sqrt(mean(((pred-obs)*(pred-mod)), na.rm = na.rm))}

read the files(note that now dir3 is added and files have the same 
attributes):

         dir1 <- list.files("/donnees/notghi", "*.img", full.names = TRUE)
         dir2 <- list.files("/donnees/baieSt", "*.img", full.names = TRUE)
         dir3 <- list.files("/donnees/modt", "*.img", full.names = TRUE)
  code:
I tried to do this,I put `3` instead of `2`:

         file_tot<-array(dim=c(1440,720,600,3))## worked well

   and then change this line accordingly:
from:

        result<-apply(file_tot,c(1,2),function(x){bias(x[,1],x[,2])})

to :

       result<-apply(file_tot,c(1,3),function(x){err(x[,1],x[,2],x[,3])})
          ## not 3 instead of 2 and I added x[,3]
               Is that right?

This worked without errors but I checked the results and they were crap.

-- 
Amen Alyaari, UPMC
PhD student
Unit of Functional Ecology&  Environmental Physics [EPHYSE]
National Institute of Agricultural Research [INRA].
71, Avenue Edouard Bourlaux
33140 Villenave d'Ornon
Téléphone : +33(0) 5 57 12 24 27
Fax : +33 (0)5 57 12 24 20
FRANCE



More information about the R-help mailing list