I have three datasets and would like co know how much N was used in my calculations.
I read the data into a multi-dimensional array with dimensions (nx, ny, ntsteps, ndatasets), e.g. with a smaller example dataset:
# nx ny nsteps ndatasets
dat = runif(20 * 30 * 100 * 3)
dim(dat) = c(20, 30, 100, 3)
> str(dat)
num [1:20, 1:30, 1:100, 1:3] 0.1834 0.8537 0.0672 0.0734 0.8078 ...
we take advantage of the cor
functions and build this function to compute how many N we have:
cor_withN <- function(...) {
res <- try(cor.test(...)$parameter+2, silent=TRUE)
ifelse(class(res)=="try-error", NA, res)}
Now we take advantage of the fact that apply also works on multi-dimensional arrays, not only matrices:
We use apply to iterate over all the x,y,z triples.
result = apply(dat, c(1,2), function(x) cor_withN(x[,1], x[,2],x[,3]))
> str(cor_result)
logi [1:20, 1:30] NA NA NA NA NA NA ..
so something is wrong by getting NA NA NA NA if the last line went well! then
str(cor_result)
should be
logi [1:20, 1:30] 100 100 100 100 100 ..(nsteps)
Any idea on why I am getting NA or is there another way to do it?
When I tested it with 2 datsets,it went well!
cor_result = apply(dat, c(1,2), function(x) cor_withN(x[,1], x[,2]))
> str(cor_result)
num [1:20, 1:30] 100 100 100 100 100 100 100 100 100 100
so the problem is when I added x[,3]
!!
Thanks