mardi 29 mai 2018

R round product between positive real numbers x and y to between 0 and 1 efficiently

I am having this function to make products between two positive number that returns the product if this it less or equal to 1, otherwise returns 1.

f1 <- function(x, y) ifelse(x*y <= 1, x*y, 1)

It annoys me that I have to do the x*y calculation twice - is there a base R function that can do this, or another way to do the task ? I am aware that the difference in computing time perhaps is small (is it O vs 2*O ?) but still ... and out of curiosity.

Aucun commentaire:

Enregistrer un commentaire