<div dir="ltr"><div>Evelyn e demais colegas, boa tarde!</div><div><br></div><div>Acredito que basicamente faltou atribuir o índice (i) dos elementos a serem usados em cada rodada ou verificação. Pode ser resolvido com uso de loops ou não. No código abaixo tem três opções que testei.</div>
<div><br></div><div><font face="courier new, monospace">### <code r></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">Sit=c(3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, </font></div>
<div><font face="courier new, monospace"> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">d1=c(1.27, 5.80, 2.42, 3.11, 5.25, 4.74, 5.42, 5.14, 5.27, 5.61, 6.09, 6.38, 6.34, 8.60,</font><span style="font-family:'courier new',monospace"> 7.36, 9.96, 10.86, 11.35, 8.94, 9.19, 8.66, 8.98, 9.86, 9.94, 11.59, 15.61,</span></div>
<div><font face="courier new, monospace"> 6.28, 7.83, 7.94, 7.67, 8.62, 9.43, 9.00)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">d2=c(9.12, 10.81, 10.21, 7.11, 7.23, 7.62, 7.69, 7.59, 5.73, </font></div>
<div><font face="courier new, monospace"> 5.39, 5.35, 5.42, 5.32, 3.84, 2.89, 0.70, 1.65, 2.32, 4.01,</font></div><div><font face="courier new, monospace"> 4.16, 4.32, 4.74, 5.12, 3.01, 1.91, 7.33,</font></div>
<div><font face="courier new, monospace"> 3.81, 2.27, 2.22, 4.44, 4.76, 7.35, 7.35)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">#############</font></div>
<div><font face="courier new, monospace">### Solução 1</font></div><div><font face="courier new, monospace">#############</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">for(i in seq_along(Sit)){</font></div>
<div><font face="courier new, monospace"> if (i==1) k1 <- as.vector(NULL)</font></div><div><font face="courier new, monospace"> if(Sit[i]==1) {ktmp <- (100 + d1[i]^2 - d2[i]^2) / 20}</font></div>
<div><font face="courier new, monospace"> else if(Sit[i]==2) {ktmp <- (10 + (100 + d1[i]^2 - d2[i]^2) / 20)}</font></div><div><font face="courier new, monospace"> else {ktmp <- (400 + d1[i]^2 - d2[i]^2) / 40}</font></div>
<div><font face="courier new, monospace"> k1 <- c(k1,ktmp)</font></div><div><font face="courier new, monospace">}</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">#############</font></div>
<div><font face="courier new, monospace">### Solução 2</font></div><div><font face="courier new, monospace">#############</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">for(i in seq_along(Sit)) {</font></div>
<div><font face="courier new, monospace"> if (i==1) k2 <- as.vector(NULL)</font></div><div><font face="courier new, monospace"> ktmp <- (switch(Sit[i], (100 + d1[i]^2 - d2[i]^2) / 20,</font></div><div>
<font face="courier new, monospace"> (10 + (100 + d1[i]^2 - d2[i]^2) / 20),</font></div><div><font face="courier new, monospace"> (400 + d1[i]^2 - d2[i]^2) / 40))</font></div>
<div><font face="courier new, monospace"> k2 <- c(k2,ktmp)</font></div><div><font face="courier new, monospace">}</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">#############</font></div>
<div><font face="courier new, monospace">### Solução 3</font></div><div><font face="courier new, monospace">#############</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">{</font></div>
<div><font face="courier new, monospace"> df <- data.frame(Sit, d1, d2)</font></div><div><font face="courier new, monospace"> df$k3 <- NA; df</font></div><div><font face="courier new, monospace"> i <- which(Sit==1); df$k3[i] <- (100 + d1[i]^2 - d2[i]^2) / 20</font></div>
<div><font face="courier new, monospace"> i <- which(Sit==2); df$k3[i] <- (10 + (100 + d1[i]^2 - d2[i]^2) / 20)</font></div><div><font face="courier new, monospace"> i <- which(Sit==3); df$k3[i] <- (400 + d1[i]^2 - d2[i]^2) / 40</font></div>
<div><font face="courier new, monospace"> df</font></div><div><font face="courier new, monospace"> k3 <- df$k3</font></div><div><font face="courier new, monospace">}</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">all.equal(k1, k2, k3)</font></div><div><font face="courier new, monospace"># [1] TRUE</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">### </code></font></div>
<div class="gmail_extra"><br clear="all"><div><div dir="ltr">Éder Comunello <<a href="mailto:comunello.eder@gmail.com" target="_blank">c</a><a href="mailto:omunello.eder@gmail.com" target="_blank">omunello.eder@gmail.com</a>> <br>
Dourados, MS - [22 16.5'S, 54 49'W]<br></div></div>
<br></div></div>