I want to replace apply() (and it's derivatives) with a purrr function.

I have a data.frame like this one:

> df
  V1 V2 V3
1 NA  2  3
2  2 NA  3
3  3  3 NA

And I want to apply two functions rowwise: min(x, na.rm = T) and which.min(x)and return the results as a dataframe.

If I know how many columns there are I can do e.g. this:

pmap_dfr(df, function(V1, V2, V3) {data.frame(min = pmin(V1, V2, V3, na.rm = T),
                                              where = which.min(c(V1, V2, V3)))})

  min where
1   2     2
2   2     1
3   3     1

How can I make pmap() or any other purrr function take the whole row as argument just like apply()does?

func <- function(x) {data.frame(min = min(x, na.rm = T), where = which.min(x))}

> Reduce(rbind, apply(df,1, func))
    min where
V2    2     2
V1    2     1
V11   3     1


I probably just missed a feature or some trick. Thanks for your help.

3 Answers

3
H 1 On Best Solutions

Your solution will work for all columns if you use an ellipsis.

pmap_dfr(df, ~data.frame(min = min(..., na.rm = TRUE), where = which.min(c(...)))) 

  min where
1   2     2
2   2     1
3   3     1
2
tmfmnk On

One possibility could be:

df %>%
 mutate(min = invoke(pmin, na.rm = TRUE, .),
        where = max.col(!is.na(-.)[, 1:length(.)], ties.method = "first"))

  V1 V2 V3 min where
1 NA  2  3   2     2
2  2 NA  3   2     1
3  3  3 NA   3     1

Or if you want to keep just the last the two columns:

df %>%
 transmute(min = invoke(pmin, na.rm = TRUE, .),
        where = max.col(!is.na(-.)[, 1:length(.)], ties.method = "first"))
1
Wimpel On

Not a purrr-solution, but a data.table oneliner.

library(data.table)

dt <- fread("row V1 V2 V3
1 NA  2  3
2  2 NA  3
3  3  3 NA")

melt(dt, id.vars = "row")[ , .SD[which.min(value)], by = row]
   row variable value
1:   1       V2     2
2:   2       V1     2
3:   3       V1     3