Write an equation in R and store the result in a vector
Here is the equation
This is what i have thought so far. I am not sure if this is correct.
result <- vector("double", 10)
for (t in 3:-1) {
for (d in 0:1) {
print(((exp(1)^(1 * (t - d))) / (1 + exp(1)^(t - d))))
}
}
I also want to store these values in a vector. How can i do that?
Solution 1:
Try:
result <- vector("double", 10)
i <- 0
for (t in 3:-1) {
for (d in 0:1) {
i <- i + 1
result[i] <- ((exp(1)^(1 * (t - d))) / (1 + exp(1)^(t - d)))
print(((exp(1)^(1 * (t - d))) / (1 + exp(1)^(t - d))))
}
}
#> [1] 0.9525741
#> [1] 0.8807971
#> [1] 0.8807971
#> [1] 0.7310586
#> [1] 0.7310586
#> [1] 0.5
#> [1] 0.5
#> [1] 0.2689414
#> [1] 0.2689414
#> [1] 0.1192029
result
#> [1] 0.9525741 0.8807971 0.8807971 0.7310586 0.7310586 0.5000000 0.5000000
#> [8] 0.2689414 0.2689414 0.1192029
Created on 2022-01-13 by the reprex package (v2.0.1)