Jonsson
2012-Dec-03 15:15 UTC
[R] How to calculate the spatial correlation of several files?
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.
Rui Barradas
2012-Dec-03 16:12 UTC
[R] How to calculate the spatial correlation of several files?
Hello, Inline. Em 03-12-2012 15:15, Jonsson escreveu:> 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.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> 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. > > ______________________________________________ > R-help at 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.
Jonsson
2012-Dec-03 16:26 UTC
[R] How to calculate the spatial correlation of several files?
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> for (.f in seq_along(dir1)){
+ 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.
Rui Barradas
2012-Dec-03 16:39 UTC
[R] How to calculate the spatial correlation of several files?
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:> 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
>> for (.f in seq_along(dir1)){
> + 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.
>
> ______________________________________________
> R-help at 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.
Jonsson
2012-Dec-04 07:59 UTC
[R] How to calculate the spatial correlation of several files?
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.