I have a grid. For each element of the grid, I'm drawing symbols depending on their contents. I did:
for (i in 1:nx) { # to each column
x1 <- xmin + (i-1)*size #size is the lateral size of the quadricules = 0.5
x2 <- xmin + i*size
for (j in 1:ny) { # to each row
y1 <- ymin + (j-1)*size
y2 <- ymin + j*size
nind <- nrow(mam[which(mam$LAT >= y1 & mam$LAT < y2 & mam$LON >= x1 & mam$LON < x2),]) # number of rows in table "mam" inside the given limits (x1,y1,x2,y2)
if (nind == 1) {
rect(x1+gap,y1+gap,x2-gap,y2-gap) # gap is about 0.1
segments(x1+gap,y1+gap,x2-gap,y2-gap)
} else if (nind == 2) {
rect(x1+gap,y1+gap,x2-gap,y2-gap)
segments(x1,y2,x2,y1)
} else if (nind == 3) {
rect(x1+gap,y1+gap,x2-gap,y2-gap)
segments(x1,y1,x2,y2)
segments(x1,y2,x2,y1)
}
}
}
It works ok, and produces
forget about the colors. It's the small black squares that are being drawn here. But it takes too long, and for some operations apply seems faster (although some people say that's not true anymore... I'm not sure).
My question is: how can I do that using apply? I need the "i" parameter inside the function, so I know where the squares will be drawn. But haven't found how to get it. In other similar questions, the answers were always about using some other function, like outer() or R natural vectorization... but I don't think those will work here. Thanks in advance!