简体   繁体   English

R:找到符合条件的行

[英]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: 这显然是一个条件本身,但我不知道如何在R中制定它。我到目前为止的代码是:

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. 它将值0.01分配给满足该条件的行的第7列。 I would like to also make column 7 of the row below the selected rows = 0.1. 我还想在选定行= 0.1下面的行的第7列。

Any help would be greatly appreciated! 任何帮助将不胜感激!
Thanks 谢谢
Mike 麦克风

Maybe with which ? 也许与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 : 编辑:关注SimonO101评论,如果你想修改符合条件的行和下面的行,你只需要替换:

index <- index+1

By : 通过:

index <- c(index, index+1)

Subsetting in R uses logical vectors. R中的子集使用逻辑向量。 That means you can shift the logical vector by one position. 这意味着您可以将逻辑向量移动一个位置。 Example: 例:

set.seed(42)
DF <- data.frame(x=1:10, y=rnorm(10))
#     x           y
# 1   1  1.37095845
# 2   2 -0.56469817
# 3   3  0.36312841
# 4   4  0.63286260
# 5   5  0.40426832
# 6   6 -0.10612452
# 7   7  1.51152200
# 8   8 -0.09465904
# 9   9  2.01842371
# 10 10 -0.06271410

ind <- DF$y < 0
#Shift by one position:
ind <- c(FALSE, head(ind,-1))
DF[ind,]
#  x         y
#3 3 0.3631284
#7 7 1.5115220
#9 9 2.0184237

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 R 从满足每个 vendor_id 多个条件的大型数据集中提取行 - Using R for extracting rows from a large dataset which meet multiple conditions for each vendor_id 如何在R Shiny中选择必须满足多重条件的矩阵行 - How to select rows of a matrix which has to meet mutiple conditions in R Shiny 组合满足R中条件的行 - Combining rows which meet a criterion in R 在 R 中,如何只删除满足特定条件的特定百分比的行? - In R, how to drop only a certain percentage of rows that meet certain conditions? 在 R 中选择满足特定条件的特定行和以下行 - Selecting specific rows and following ones which meet certain criteria in R 从满足R中条件的行中提取值 - Extracting values from rows which meet a condition in R 如何删除R中存在重复并满足另一个条件的行? - How to delete rows which have duplicates and meet another condition in R? 如何计算r中哪些行满足条件? - How to count which rows are meeting conditions in r? R 数据帧:select 行在按名称索引的多列(变量)上满足逻辑条件 - R data frame: select rows that meet logical conditions over multiple columns (variables) indexed by name 在R中使用coldate函数保留满足依赖于其他列的条件的行 - Keeping rows that meet conditions that depend on other columns using coldate function in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM