Checking at least one value in a data row is greater than a given threshold for a particular row

This is my small reproducible example for a dataset I'm working on:

set.seed(123)
dat <- as.data.frame( cbind(a=1+round(runif(5), 2), b=round(rnorm(5), 2), high_cutoff=round(1+rnorm(5), 1)) )

Information frame:

     a     b   high_cutoff
   1.29 -1.69         2.3
   1.79  1.24        -0.7
   1.41 -0.11         2.7
   1.88 -0.12         1.5
   1.94  0.18         3.5

I am trying to check if there is at least one value in the first two columns in the row that is higher than the correction threshold in the third column (let's say that I want to keep 1 if either of the two values ​​is higher than the cutoff).

In this example, I should find the following:

   higher_than_cutoff         
0
1
0 
1
0

I tried to use the following (incorrect) code and some of its variants without much success:

higher_than_cutoff <- apply( dat[, c("a", "b")], 1, function(x) any(x > dat[, "high_cutoff"]) )

Can you give some tips on how to proceed? Any help is much appreciated

+4
source share
4 answers

( TRUE/FALSE + )

+(rowSums(dat[-3L] > dat[, 3L]) > 0)
## [1] 0 1 0 1 0

apply, -

apply(dat, 1, function(x) +(any(x[-3] > x[3])))
## [1] 0 1 0 1 0
+5

 as.integer(do.call(pmax,dat[-3]) > dat[,3])
 #[1] 0 1 0 1 0

((max.col(dat))!=3)+0L
  #[1] 0 1 0 1 0
+5

The desired result can be obtained using

higher_than_cutoff <- apply(dat,1,function(x) (max(x[1],x[2])>x[3])*1)
+3
source

Perhaps I misunderstood what you want to achieve, but the desired result can be obtained without using apply, we just compare the full column vectors, no server operations.

+(dat$a > dat$high_cutoff | dat$b > dat$high_cutoff)
# [1] 0 1 0 1 0
+1
source

All Articles