samedi 28 novembre 2020

Writing a thresholding function in R

I'm trying to make a function so that every value x in a dataframe df is replaced by 1 if above y, otherwise it's replaced by 0.

I tried

threshold <- function(x, y) if (x < y) {
  x <- 0 
  } else {
    x <- 1
}

using it, returns a warning:

> threshold(calcium, calcium.threshold)
Warning message:
In if (x < y) { :
  the condition has length > 1 and only the first element will be used

And I'm not sure why?

Cheers.

Aucun commentaire:

Enregistrer un commentaire