I am new using R and while working on a research for college I wrote this code:
pixel <- function(a){z <- 22-a%/%22 ; y <- a-22*a%/%22
b <- c(y, z)
return(b)}
a <- c(339, 339, 270, 383, 205, 269, 275, 413, 203, 364, 365, 317, 318, 342, 360, 408, 319, 278, 339, 317, 294)
archivos <- list.files(pattern = "sR.")
for (j in 1:length(archivos)) {
assign("n", nc_open(archivos[j]))
assign("r", ncvar_get(eval(n), "pcp"))
assign(sprintf("o%d", j), c(1:length(a)))
for (i in 1:length(a){
y <- pixel(a[i])[1]
z <- pixel(a[i])[2]
eval(as.name(sprintf("o%d", j)))[i] <- r[y,x]
}
}
I intend to store the output of the pixel
function into a vector created for each value of the j
loop.
I believe the last part of the code is the problem and I tested the following code:
> eval(as.name(sprintf("o%d", 1)))[1]
[1] 89
I can retrieve the first value of the vector o1
, which is 89, but when trying to put a new value in that position R doesn't allow me to:
> eval(as.name(sprintf("o%d", 1)))[1]<-1
Error in eval(as.name(sprintf("o%d", 1)))[1] <- 1 :
target of assignment expands to non-language object
The final output are empty vectors (o1
, o2
, etc.). I've been looking for different approaches to do this but I am stuck.
I would be glad to hear suggestions.