Janis S.
Janis S.

Reputation: 47

Divide a vector by different values based on the result of the division

I have a Df like this:

          x         y          z
  <dbl>     <dbl>      <dbl>
1  408001.9       343          0
2  407919.2       343          0
3  407839.6       343          0
4  407761.2       343          0
5  407681.7       343          0
6  407599.0       343          0
7  407511.0       343          0
8  407420.5       343          0
9  407331.0       343          0
10 407242.0       343          0
11 407152.7       343          0
12 407062.5       343          0
13 406970.7       343          0
14 406876.6       342          0
15 406777.1       342          0
16 406671.0       342          0
17 406560.9       342          0
18 406449.4       342          0
19 406339.0       342          0
20 406232.5       342          0
...  ...          ...         ...

with x decreasing.

And a vector like

vec=(a1, a2, a3, a4, a5, a6, ...)

with a1< a2< a3< a4...

Now I want to divide df$x by vec[1], what will give the same result (rounded) as for df$y.

But now, when the value in df$z drops by one to 342, I want to divide the value in df$x by vec[2] from then on, to get the new df$z values.

From here the result will be different from df$y, as for df$y the number to divide with is allways vec[1]and will not change

Every time the value I get for df$z drops by one, the next values for df$z shal be calculated with the corresponding vec[i] where i is the number of drops+1 so far

In the end I want a vector df$z, where the values are df$x / vec[i], where vec [i] depends on, what the last number of df$z is.

reproducible example:

test <- data.frame(x = sort((seq(500, 600, 2)), decreasing = T)
                   )

vec <- seq(10, 10.9, 0.03)

for(i in 1:31){
  test[i+1] <- round(test$x/vec[i])
}

This will give you a df with one col for every value of vec, that test$x got divided by. Now, in the end, my vector shall contain the values of col2 until the value in col2 drops from 60 to 59. Afterwards I want the values from col3 until the value in col3 drops below 59 to 58. Then I want the values from col4 and so on.

How can I achive this with any data(like mine above, which is not linear ditributed as this example.)

I tried some for and while loops, but none worked. I didn't even get close to what I want.

I think my problem is that I dont know how to make the condition depenent on a value(the value of df$z at point i), that I want to calculate in the same operation. I want to calculate the value of df$z[i] with the value of vec[t], that has been used so far. But if the value of df$z drops by one at a certain observation[i], the value of vec[t+1] shall be used for the division from then on.

Thanks for your help.

Upvotes: 0

Views: 72

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18425

I hope I've understood what you are asking. This might be it...

test <- data.frame(x = sort((seq(500, 600, 2)), decreasing = T)
vec <- seq(10, 10.9, 0.03)

#this function determines the index of `vec` to use
xcol<-function(v){
  x<-rep(NA,length(v))
  x[1] <- 1
  for(i in 2:length(v)){
    x[i] <- x[i-1]
    if(round(v[i]/vec[x[i]])<round(v[i-1]/vec[x[i]])){
      x[i] <- x[i]+1
    }
  }
  return(x)
}

test$xcol <- xcol(test$x)
test$z <- round(test$x/vec[test$xcol])

 test
     x xcol  z
1  600    1 60
2  598    1 60
3  596    1 60
4  594    2 59
5  592    2 59
6  590    2 59
7  588    2 59
8  586    3 58
9  584    3 58
10 582    3 58
11 580    3 58
12 578    4 57
...

Upvotes: 1

Related Questions