R:Finding rows which meet conditions

13,353

Maybe with which ?

index <- which(decisionMatrix[,1] == 1 & decisionMatrix[,9] == 1)
## Shift indices by 1
index <- index+1
## Remove an index that would be greater than the number of rows
index <- index[index<=nrow(decisionMatrix)]
decisionMatrix[index,7] <- .01

EDIT : Following SimonO101 comment, if you want to modify both the rows matching conditions and the rows below, you just have to replace :

index <- index+1

By :

index <- c(index, index+1)
Share:
13,353
Mike
Author by

Mike

Updated on June 14, 2022

Comments

  • Mike
    Mike almost 2 years

    I've seen a few threads on this and have formulated a semi-answer, but what I require is slightly different from what I've seen. I'm looking to find the row BELOW a row which meets certain conditions. That is obviously a condition in and of itself, but I don't know how to formulate it in R. The code I have so far is:

    index = decisionMatrix[,1] == 1 & decisionMatrix[,9] == 1  
    decisionMatrix[index,7] = .01  
    

    which assigns the value 0.01 to column 7 of rows that meet that condition. I would like to also make column 7 of the row below the selected rows = 0.1.

    Any help would be greatly appreciated!
    Thanks
    Mike