R: use min() within dplyr::mutate()

user3375672 picture user3375672 · Jan 21, 2015 · Viewed 12.4k times · Source
require(plyr)
require(dplyr)    
set.seed(8)
    df <- 
      data.frame(
        v1 = runif(10, -1,1),
        v2 = runif(10, -1,1))

The problem: How can I get the correct values into the min() function as part of mutate()- basically, I would like to assign v3as v1 divided with the smallest of v1 and v2. This doesnt work:

  df <- 
         df %>% mutate(v3=ifelse(v1 !=0, v1/min(v1,v2), 0))

I guess I am missing something really simple.

Answer

talat picture talat · Jan 21, 2015

From the help page on ?min:

pmax and pmin take one or more vectors (or matrices) as arguments and return a single vector giving the ‘parallel’ maxima (or minima) of the vectors.

On the other hand:

max and min return the maximum or minimum of all the values present in their arguments

So you want to use pmin here. With dplyr, one option - as commented above - is like this:

df %>% mutate(v3 = (v1 != 0) * v1/pmin(v1,v2))

Nice side effect here is that you can avoid using ifelse and just mulitply with the logical vector (TRUE / FALSE which is then converted to 1 / 0).