Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R:Finding rows which meet conditions

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

like image 928
Mike Avatar asked Dec 07 '25 21:12

Mike


1 Answers

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)
like image 116
juba Avatar answered Dec 09 '25 09:12

juba