R:查找满足条件的行

标签 r conditional-statements

我已经看到了一些与此相关的主题并制定了一个半答案,但我需要的与我所看到的略有不同。我正在寻找满足特定条件的行下方的行。这显然本身就是一个条件,但我不知道如何在 R 中表达它。我目前的代码是:

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

它将值 0.01 分配给满足该条件的行的第 7 列。我还想使所选行下方的行的第 7 列 = 0.1。

如有任何帮助,我们将不胜感激!
谢谢
迈克

最佳答案

也许用 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

编辑: 在 SimonO101 评论之后,如果你想同时修改行匹配条件和下面的行,你只需要替换:

index <- index+1

作者:

index <- c(index, index+1)

关于R:查找满足条件的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19312895/

相关文章:

R Parallel,每次使用并行应用时创建一个新集群会更好吗?

r - 修改 cSplit_e 函数以考虑多个值

r - geom_points 的选择性抖动

java - 查询 DynamoDB

vb.net - 在 VB.NET 中选择 Case Fall through with Not Condition

php - while(false !== ($f=readdir($d))){ 的解释

sql - 无法使用 R 中的 sqlSave 附加到 SQL Server 表

loops - LISP文件读取循环条件和序号

Mysql根据条件计算列值

r - 在 ODE 中使用循环以图形方式比较不同的参数 R