简体   繁体   English

使用R中特定列及其前一列的值检查条件

[英]Check conditions using the value of a specific column and its previous column by row in R

I'm trying to create an output where a given condition is met: - the value in a given column must be 1 - the value in the previous column must be -1 我正在尝试创建满足给定条件的输出:-给定列中的值必须为1-上一列中的值必须为-1

As example, the input will be: 例如,输入将是:

> cc <- matrix(c(1,-1,NA,-1,1,-1),  nrow=2, byrow = T)
> cc
     [,1] [,2] [,3]
[1,]    1   -1   NA
[2,]   -1    1   -1

I want to get an output with the same dimensions as my input and, in case the condition above is met, it will return "TRUE"; 我想获得与输入尺寸相同的输出,并且如果满足上述条件,它将返回“ TRUE”; In case the condition is not met, the output should return FALSE or NA. 如果不满足条件,则输出应返回FALSE或NA。 As example: 例如:

> output<- matrix(c(NA,FALSE,NA,NA,TRUE,FALSE), nrow=2, byrow=T)
> output
     [,1]  [,2]  [,3]
[1,]   NA FALSE    NA
[2,]   NA  TRUE FALSE

The first column has NAs because we don't have a previous column but it can also be FALSE. 第一列具有NA,因为我们没有上一列,但也可以为FALSE。

Here is a way to get the desired output: 这是获取所需输出的一种方法:

> cbind(NA, cc[, -1, drop = F] == 1 & cc[, -ncol(cc), drop = F] == -1)
     [,1]  [,2]  [,3]
[1,]   NA FALSE    NA
[2,]   NA  TRUE FALSE

apply is not necessary. apply是没有必要的。 the drop = F is used to maintain the shape of the matrix in case it has only two columns. 如果只有两列,则drop = F用于维持矩阵的形状。

Another way: 其他方式:

> x <- seq_len(ncol(cc))
> cc[, c(NA, x[-1])] == 1 & cc[, c(NA, head(x, -1))] == -1
     [,1]  [,2]  [,3]
[1,]   NA FALSE    NA
[2,]   NA  TRUE FALSE

Creating data frame 创建数据框

cc <- data.frame(matrix(c(1,-1,NA,-1,1,-1),  nrow=2, byrow = T))

cc
   X1  X2  X3
1   1  -1  NA
2  -1   1  -1

Looping through the data frame, changing row value based on value from previous column 遍历数据帧,根据上一列中的值更改行值

# Loop starts from the last column
for(i in length(colnames(cc)):1){
    # Nested loop starts from first row
    for(j in 1:length(rownames(cc))){
        # Check if column is the first
        if(i == 1){
            cc[j,i] <- NA
        }
        # Check for NAs
        else if(is.na(cc[j,i]) | is.na(cc[j,i-1])){
            cc[j,i] <- NA
        }
        # Set to true if condition is met
        else if(cc[j,i] == 1 & cc[j,i-1] == -1){
            cc[j,i] <- TRUE
        } 
        # Else set to false
        else(cc[j,i] <- FALSE)
    }
}

This can obviously be further optimized, but I tried to keep the code as simple as possible. 显然,这可以进一步优化,但是我尝试使代码尽可能简单。

Result 结果

cc
   X1  X2  X3
1  NA   0  NA
2  NA   1   0

Explanation 说明

I think you might have a logical error in your approach. 我认为您的方法可能存在逻辑错误。 This matrix cannot be looped through in sequential order (from column 1 to column n) and produce a reasonable result. 此矩阵不能按顺序循环(从第1列到第n列)并产生合理的结果。 If you start in column 1 and set it to NA (as it has no previous column) you will get 如果您从第1列开始并将其设置为NA (因为它没有上一列),您将获得

     [,1] [,2] [,3]
[1,]   NA   -1   NA
[2,]   NA    1   -1

In the next loop through column 2 you will inevitably get all NAs as the previous column is all NA (and not -1), and so forth. 在下一遍通过第2列的循环中,您将不可避免地获得所有NAs因为上一列是所有NA (而不是-1),依此类推。

     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM