Skip to content

Instantly share code, notes, and snippets.

@batpigandme
Last active February 27, 2020 02:48
Show Gist options
  • Save batpigandme/667897e67292e0c9e0afda6d021f292c to your computer and use it in GitHub Desktop.
Save batpigandme/667897e67292e0c9e0afda6d021f292c to your computer and use it in GitHub Desktop.
Same operation done using: rowwise(), pmin(), pmap_dbl()
library(tidyverse)
set.seed(406)
df <- tibble(x = runif(3), y = runif(3), z = runif(3))
df %>% rowwise() %>% mutate(m = min(c(x, y, z)))
#> # A tibble: 3 x 4
#> # Rowwise: 
#>       x     y     z     m
#>   <dbl> <dbl> <dbl> <dbl>
#> 1 0.975 0.151 0.312 0.151
#> 2 0.482 0.817 0.850 0.482
#> 3 0.900 0.805 0.995 0.805
df %>% mutate(m = pmin(x, y, z))
#> # A tibble: 3 x 4
#>       x     y     z     m
#>   <dbl> <dbl> <dbl> <dbl>
#> 1 0.975 0.151 0.312 0.151
#> 2 0.482 0.817 0.850 0.482
#> 3 0.900 0.805 0.995 0.805
df %>% mutate(m = pmap_dbl(list(x, y, z), min))
#> # A tibble: 3 x 4
#>       x     y     z     m
#>   <dbl> <dbl> <dbl> <dbl>
#> 1 0.975 0.151 0.312 0.151
#> 2 0.482 0.817 0.850 0.482
#> 3 0.900 0.805 0.995 0.805

Created on 2020-02-26 by the reprex package (v0.3.0.9001)

library(tidyverse)
set.seed(406)
df <- tibble(x = runif(3), y = runif(3), z = runif(3))
df %>% rowwise() %>% mutate(m = min(c(x, y, z)))
df %>% mutate(m = pmin(x, y, z))
df %>% mutate(m = pmap_dbl(list(x, y, z), min))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment