The loop below modifies bloc. Can sapply() achieve the same result?
Or in other words, can sapply() update variables in the base environment? I tried using assign() but without success. Thank you.
n <- 100
fencePosts <- c(17,34)
bloc <- rep(0,n)
for (i in 1:length(fencePosts)){
bloc[fencePosts[i]:n] = i
}
table(bloc)
I was thinking of something like the following line, but of course bloc is out of scope (although I’m not sure why this doesn’t raise a “bloc not found” error).
zilch <- sapply(1:length(fencePosts),function(i)bloc[fencePosts[i]:n] = i)
I don’t know how kosher it is to the R-perts, but you could do