我试图检查向量中的每个值是否小于或等于2. assert_这似乎有一个问题(可能是由于我缺乏理解)。有人能告诉我这里有什么不对吗?
> require(assertthat)
> x<-c(1,2,1.5)
> assert_that(x<=2) #should return true
Error: assert_that: assertion has length greater than 1
> y<-c(1,2,1.5,3,3)
> assert_that(y<=2) #should return false
Error: assert_that: assertion has length greater than 1
> stopifnot(x<=2)
答案 0 :(得分:3)
只是说
assert_that(y<=2)
含糊不清。你想所有 y<=2
吗?您想要至少一个 y<=2
吗?您可以分别使用assert_that(all(y<=2))
或assert_that(any(y<=2))
折叠为单个断言。