Skip to content

How to calculate the spatial correlation of several files?

6 messages · Jonsson, Rui Barradas

#
dir1 <- list.files("C:\\Users\\aalyaari\\Desktop\\cor", "*.bin",
full.names = TRUE)
    dir2 <- list.files("C:\\Users\\aalyaari\\Desktop\\cor2", "*.bin",
full.names = TRUE)
    results <- list()
        for (.files in dir1){   # read in the 365 files as a vector of
numbers for dir1
        file1 <- do.call(rbind,(lapply(.files, readBin  , integer() , size =
2 ,
                                    n = 360 * 720 , signed = T)))    }
        for (.files in dir2){   # read in the 365 files as a vector of
numbers for dir2
        file2<- do.call(rbind,(lapply(.files, readBin  , integer() , size =
2 , 
                        n = 360 * 720 , signed = T)))   }
       # Now each file  in both directories is a vector. I am not sure how
to tell R to correlate the first column in dir1 to the correspond column
from dir2. we will finally get only one spatial correlation map.
I tried to this:
 # calculate the  correlation so we will get a correlation map
    for (.files in seq_along(dir1)){              
        results[[length(results) + 1L]]<- cor(file1 ,file2)
        }
I got error:Error in cor(file1, file2) : allocMatrix: too many elements
specified`





--
View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-spatial-correlation-of-several-files-tp4651888.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,

Inline.
Em 03-12-2012 15:15, Jonsson escreveu:
Are you sure? Shouldn't your file reading routines be

do.call(rbind, lapply(dir1, readBin, integer(), size = 2, n = 360 * 720, 
signed = T))
do.call(rbind, lapply(dir2, readBin, integer(), size = 2, n = 360 * 720, 
signed = T))


to lapply readBin to each file in dir1/dir2?

Anyway, to correlate the first row in file1 to the first row in file2, 
etc, try

for (.f in seq_along(dir1)){
     results[[.f]]<- cor(file1[, .f] ,file2[, .f])
}


Hope this helps,

Rui Barradas
#
Hello,

Sorry, made a mistake.
Em 03-12-2012 16:12, Rui Barradas escreveu:
Here. it should be "column" not "row".

Rui Barradas
#
Thanks
you meant it shoud be:
file1=do.call(rbind, lapply(dir1, readBin, integer(), size = 2, n = 360 *
720, 
                      signed = T)) 
file2=do.call(rbind, lapply(dir2, readBin, integer(), size = 2, n = 360 *
720, 
                      signed = T))
Please see the error
+   results[[.f]]<- cor(file1[, .f] ,file2[, .f]) 
+ } 
Error in file2[, .f] : incorrect number of dimensions



--
View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-spatial-correlation-of-several-files-tp4651888p4651901.html
Sent from the R help mailing list archive at Nabble.com.
#
Ok, so I was mistaken when I thought I had made a mistake. Change to 
"rows" again:

for (.f in seq_along(dir1)){
     results[[.f]]<- cor(file1[.f, ] ,file2[.f, ])
}


Rui Barradas
Em 03-12-2012 16:26, Jonsson escreveu:
#
So where is the final correlation map
Can we write it:
to.write =
file(paste("C:\\Users\\aalyaari\\desktop\\corr1.bin",sep=""),"wb")

writeBin(as.double(results[[.f]]), to.write, size = 4)




--
View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-spatial-correlation-of-several-files-tp4651888p4652004.html
Sent from the R help mailing list archive at Nabble.com.