0

I would like to understand how I can get the row-wise minimum for a set of columns that are defined in a vector of strings, i.e. how to get the following output with the following input:

Input:

t <- data.frame(x= c(1,2,3,4), y= c(2,3,4,5), z = c(4,5,6,7))
vars. <- c('x', 'y')

My (not working) suggestion:

t %>% rowwise %>% mutate(min_x_y = min(vars(vars.)))

Output should be:

  x y z min_x_y
1 1 2 4       1
2 2 3 5       2
3 3 4 6       3
4 4 5 7       4
Jan Janiszewski
  • 432
  • 3
  • 14
  • Aside from the part of having the column names in a vector, [this post](https://stackoverflow.com/q/31598935/5325862) has several ideas for row-wise summary stats – camille Mar 16 '20 at 12:10
  • Check out the new functionality of [`dplyr` 1.0](https://www.tidyverse.org/blog/2020/03/dplyr-1-0-0-is-coming-soon/) that includes an `across` function. Also discussed in this recent [SO post](https://stackoverflow.com/questions/60574290/apply-a-summarise-condition-to-a-range-of-columns-when-using-dplyr-group-by/60574568?noredirect=1#comment107166138_60574568). – caldwellst Mar 16 '20 at 12:25

3 Answers3

3

We can use pmap_dbl from purrr.

library(dplyr)
library(purrr)

t %>% mutate(min_x_y = pmap_dbl(select(., vars.), min))

#  x y z min_x_y
#1 1 2 4       1
#2 2 3 5       2
#3 3 4 6       3
#4 4 5 7       4

A base R version would be

t$min_x_y <- do.call(pmin, t[vars.])
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

Yet another alternative to the approaches already suggested would be to use a combination of tidy evaluation and pmin:

# convert character vector of variable names into symbols
vars. <- c('x', 'y') %>% dplyr::syms()

# use tidy evaluation to pass symbols to pmin inside a mutate call
t %>% 
  mutate(min_x_y = pmin(!!!vars.))
#>   x y z min_x_y
#> 1 1 2 4       1
#> 2 2 3 5       2
#> 3 3 4 6       3
#> 4 4 5 7       4
hendrikvanb
  • 459
  • 3
  • 5
0

You can do that in different ways, one is;

t <- data.frame(x= c(1,2,3,4), y= c(2,3,4,5), z = c(4,5,6,7))
vars. <- c('x', 'y')

t$min_x_Y=t(as.data.frame(t(t)) %>%
  summarise_all(funs(min)))
statistic
  • 162
  • 1
  • 13