Search code examples
rdataframematrix-indexing

subtract two columns in dataframe if a condition is met


my dataframe:

Dead4   Dead5
0       0
0       0
0       0
1       2
0       0
0       0
1       2
0       0
1       0
0       1
1       1
5      10

I want my code to say anytime Dead5 is greater than Dead4 in the same row subtract the two values and place that value in Dead5

indices<- (t$Dead5 > t$Dead4) 
t$Dead6[indices]<- (t$Dead6) - (t$Dead5)


Warning message:
In t$Dead6[indices] <- (t$Dead6) - (t$Dead5) :
  number of items to replace is not a multiple of replacement length

Can some explain what I am doing wrong and help me write a few lines of code that will do this?


Solution

  • You can do this:

    indices <- (t$Dead5 > t$Dead4) # indices is a logical vector with TRUE and FALSE
    
    t$Dead5[indices] <- (t$Dead5 - t$Dead4)[indices]
    

    It also holds for any other operation with your data.frame, like:

    t$Dead6[indices] <- (t$Dead6 - t$Dead5)[indices]
    

    If the column Dead6 exists. At each side, only the values where indices is TRUE are taken, so the replacement and replaced values are of the same length, and you don't get the warning.

    What you were doing wrong is you were giving as replacement the full (t$Dead5 - t$Dead4) vector, which is longer than the number of times the indices is TRUE (the replaced values in the left).

    R was only using the first values of your replacement vector and giving you a warning.