'How to conditionally iterate a for loop with if loop?
I want my 2nd for loop to use a conditional vector, but when I tried putting an if loop in the vector area, the code just iterates over the 1st value in the vector and leaves the rest
for (product in c('apple','mango','orange')) {
for (store in ifelse(product %in% c('mango','orange'),
c('KGS','ADA'),'ADA')) {
for (sold in c('1','2','3')) {
sp_table[nrow(sp_table)+1,] <- NA
sp_table[nrow(sp_table),'Store:id'] <- store
sp_table[nrow(sp_table),'Product:id'] <- paste(store,product,sold,sep = '_')
}
}
}
Is there any other possibility of doing this other than using an if loop before for loop, since this would require looping inside both the if and else conditions and also calling all the columns in both places.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
