I would like to run a for loop over sf with multiple conditions.
- if value of current column=100, then remains as 100;
- if value from last column >0, then value of current column = value of last column minus 10;
- else, value = 0
For example, from this:
Name | Jan | Feb | Mar | Apr |
---|---|---|---|---|
A | 100 | 0 | 0 | 0 |
B | 20 | 0 | 0 | 0 |
C | 80 | 0 | 100 | 0 |
Become this:
Name | Jan | Feb | Mar | Apr |
---|---|---|---|---|
A | 100 | 90 | 80 | 70 |
B | 20 | 10 | 0 | 0 |
C | 80 | 70 | 100 | 90 |
I wrote the following code
for (i in 3:(ncol(polys1)-1)){
if (polys1[i]==100){polys1[i]<-100}
else if (polys1[i-1]>0){polys1[i]<-(polys1[i-1]-10)}
else {polys1[i]<-0}
}
but give me the error:
Error in if (polys1[i] == 100) { :
argument is not interpretable as logical
In addition: Warning message:
In if (polys1[i] == 100) { :
the condition has length > 1 and only the first element will be used
Any help would be much appreciated!