My problem: I have an if else-statement nested in an for-loop and want to save the values for each iteration of it.
For example with some simple data, what i've tried is:
s <- c(4, 8, 3) #a string with some values
l <- list() #the list where i want the output to be saved in
for (n in 1:length(s)) {
if (n==1) {
b1 <- 1:s[n]
print(b1)
l <- c(b1)}
else {
b2 <- (s[n-1]:s[n])
print(b2)
l <- c(b1=b1, b2=b2)}}
The print() output is all vectors i want to save
[1] 1 2 3 4
[1] 4 5 6 7 8
[1] 8 7 6 5 4 3
but l only stores the first vector (from the if statement) and the last iteration of the else statement:
b11 b12 b13 b14 b21 b22 b23 b24 b25 b26
1 2 3 4 8 7 6 5 4 3
How can i save every iteration? I've been trying this for some hours and got nowhere, so any help is greatly appreciated!
Aucun commentaire:
Enregistrer un commentaire