KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to speed up/vectorize some calculations in a time series. Can I vectorize a calculation in a for loop which can depend on results from an earlier iteration? For example: z <- c(1,1,0,0,0,0) zi <- 2:6 for (i in zi) {z[i] <- ifelse (z[i-1]== 1, 1, 0) } uses the z[i] values updated in earlier steps: > z [1] 1 1 1 1 1 1 In my effort at vectorizing this z <- c(1,1,0,0,0,0) z[zi] <- ifelse( z[zi-1] == 1, 1, 0) the element-by-element operations don't use results updated in the operation: > z [1] 1 1 1 0 0 0 So this vectorized operation operates in 'parallel' rather than iterative fashion. Is there a way I can write/vectorize this to get the results of the for loop?
Tags (comma-separated)
Save Edits
Cancel