mcfly
mcfly

Reputation: 49

How to check each row according to a condition and calculate difference to next row in R

I have a dataframe with many rows and want to check each row according to a condition. If the value in column B is between 1<x<3 than I would like to calculate the difference between next row, column A - current row, column A . If the value in column B is between 2<y<4 than I would like to calculate the difference between next row, column A - current row, column A.

#      A   B   
# 1    0   1 
# 2   100  2 
# 3   150  1 
# 4   200  3 
# 5   230  1

df <- data.frame(A = c("0", "100", "150", "200", "230"), B = c("1", "2", "1", "3", "1"))

So for this example, x=150-100=50 and y=230-200=30. I tried the following approach.

    for(i in 1:nrow(df)) {
      
         if (1<df$B<3) {
            
            x = diff(df$A, lead = 1) 
            
          }
          if (2<df$B<4) {
            
            y = diff(x$A, lead = 1) 
            
          }  
          
output=c(x,y)
          
return (output)
        }

Upvotes: 0

Views: 818

Answers (1)

Jost
Jost

Reputation: 436

Hi I think you would benefit from the built in function "ifelse" which is immensely practical for conditional row replacement and such. Here are at least a solution example where the "else" statement returns NA.

df <- data.frame(A = c(0, 100, 150, 200, 230), B = c(1, 2, 1, 3, 1))

df$result <- ifelse((1 < df$B & df$B <3) | (2<df$B& df$B <4), (c(df$A[-1], 0) - df$A), NA)

Note here that I am actually "creating a new column within the function i.e. "c(df$A[-1], 0)", which simply makes the "next row minus the current" operation easier, but there is probably a smarter solution out there.

Upvotes: 2

Related Questions