<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:10pt"><div><div><font face="'times new roman', 'new york', times, serif" size="2">Olá pessoal, tenho que executar uma função parecida com essa no R. Mas ela normalmente demora longos 500 segundos para executar. Fiz uma versão que chama um código C, e essa é bem eficiente (demora cerca de 0,5 segundos),  porém gostaria de uma função totalmente em R. Caso alguém queira ajudar, desde já agradeço.</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">set.seed(1)</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">vetor1 <- rep(1:4, each = 5)</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">var1 <- abs(rnorm(4,3)) * vetor1 </font></div><div><font face="'times new roman', 'new york', times, serif" size="2">cond1
 <-abs(rnorm(4,10)) * vetor1  </font></div><div><font face="'times new roman', 'new york', times, serif" size="2">var2 <- matrix(0, nrow=length(vetor1))</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">cond2 <- matrix(0, nrow=length(vetor1))</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">for(i in 1:(length(vetor1) - 1)){</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">   if(vetor1[i] == vetor1[i+1]){</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">      var2[i] = var1[i+1]</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">      cond2[i] = cond1[i+1]</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">      </font></div><div><font face="'times new roman', 'new york', times, serif"
 size="2">   }else { </font></div><div><font face="'times new roman', 'new york', times, serif" size="2">      var2[i] = NA</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">      cond2[i] = NA  </font></div><div><font face="'times new roman', 'new york', times, serif" size="2">   }</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">   </font></div><div><font face="'times new roman', 'new york', times, serif" size="2">}</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">var2[length(vetor1)] <- NA</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">cond2[length(vetor1)] <- NA</font></div><div><font face="'times new roman', 'new york', times, serif" size="2">dados <- na.exclude(data.frame(var2, cond2))</font></div><div style="font-family: 'times new
 roman', 'new york', times, serif; font-size: 10pt; "><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 10pt; ">Gustavo Marcatti</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 10pt; ">Eng. Florestal</div></div></div></body></html>