创建一个逻辑以捕获具有两个元素的列表中的两种类型的不等式 [英] Creating a logic to capture two types of non-equality in a list with two elements

查看:38
本文介绍了创建一个逻辑以捕获具有两个元素的列表中的两种类型的不等式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有效地将​​两个逻辑合并为一个逻辑,以捕获两个相同长度元素的 list 中的两种类型的不匹配.

I want to efficiently merge two logics into one logic to capture two types of mismatches in a list with two elements of the same length.

首先,当 list()的一个元素中的 0 值不是 0 时捕获相同 list()的第二个元素中的对应位置.

First, capturing when 0 values in one element of a list() are NOT 0 for the corresponding positions in the second element of the same list().

例如,在 A 列表中, x 的前两个值是 0 ,而 y的前两个值不是 0 .因此,我想将此视为错误( stop ).

For example, in A list, the first two values of x are 0, but the first two values of y are NOT 0. So, I want to catch this as an error (stop).

但是,在 B 列表中, x 的前三个值是 0 ,而 y 也是 0 .所以,我想将此作为( warning ).

However, in B list, the first three values of x are 0, and the first three values of y are 0 as well. So, I want to catch this as a (warning).

向我此处提出了解决方案.

A solution was kindly suggested to me HERE.

第二,捕获 list() x 元素中的值不是全部相同的 OR list() y 元素中的>值 OR 并不完全相同.这些显示在列表 A1 B1 &中. C1 .

Second, capturing when values in the x element of a list() are not all the same OR values in the y element of a list() are not all the same OR both situations together. These are shown in lists A1, B1 & C1.

向我请一个解决方案此处.

似乎这两个逻辑在某种程度上是重叠的.例如,使用第一个逻辑和第二个逻辑,将列表 A 视为 error .

It seems these two logics overlap to some extent. For example, list A is considered error using the first logic AND the second logic.

问题:我们可以创建一种解决方案来合并

Question: Can we create one solution to merge this and this into one function?

# First logic examples:
( A = list(x = c(0,0,2,2), y = c(3,3,1,1)) ) # Expect error
( B = list(x = c(0,0,1,1), y = c(0,0,1,1)) ) # Expect warning
( C = list(x = c(0,1,1,0), y = c(2,1,0,0)) ) # Expect error


# Second logic examples:
( A1 = list(x = c(1,1,1,1), y = c(2,4,3,3)) ) # Expect error that says `y` is bad!
( B1 = list(x = c(1,2,1,1), y = c(3,3,3,3)) ) # Expect error that says `x` is bad!
( C1 = list(x = c(1,2,1,1), y = c(3,2,3,3)) ) # Expect error that says `x` and `y` are bad!

( D = list(x = c(1,1,1,1), y = c(3,3,3,3) ) # Expect FINE !

推荐答案

关于警告/错误的执行顺序尚不完全清楚.在下面的函数中, if 条件首先检查数据中是否存在 any 0,并且如果发现错误/警告,它将报告和 else 可能是没有零的情况,然后它将检查 unique 元素的 length ,并报告警告/错误

It is not entirely clear about the order of execution of the warning/error. In the below function, the if condition first checks if there are any 0s in the data and if it finds an error/warning, it reports and the else would be the case where there are no zeros, and then it will check for the length of unique elements and report the warning/error

check <- function(l){
  df <- as.data.frame(l)
  if(any(df == 0)){
    if(!any(rowSums(df == 0) == 1) ){
      warning("this is a warning")
    } else {
      stop("this is an error")
    }
  } else{
      v1 <- sapply(l, function(x) length(unique(x)))
      i1 <- names(which(v1 != 1))
      if(length(i1) == 1) {
          warning(paste(i1, " is bad!"))
       } else if(length(i1) > 1) {
           warning(paste(i1, collapse = ' and '), " are bad!")
       } 
  
    }
  }
  

-测试

check(A)
#Error in check(A) : this is an error
check(B)
#Warning message:
#In check(B) : this is a warning
check(C)
#Error in check(C) : this is an error
check(A1)
#Warning message:
#In check(A1) : y  is bad!
check(B1)
#Warning message:
#In check(B1) : x  is bad!
check(C1)
#Warning message:
#In check(C1) : x and y are bad!
check(D)

这篇关于创建一个逻辑以捕获具有两个元素的列表中的两种类型的不等式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆