thothal
thothal

Reputation: 20359

Apply function to a row in a data.frame using dplyr

In base R I would do the following:

d <- data.frame(a = 1:4, b = 4:1, c = 2:5)
apply(d, 1, which.max)

With dplyr I could do the following:

library(dplyr)
d %>% mutate(u = purrr::pmap_int(list(a, b, c), function(...) which.max(c(...))))

If there’s another column in d I need to specify it, but I want this to work w/ an arbitrary amount if columns.

Conceptually, I’d like something like

pmap_int(list(everything()), ...)
pmap_int(list(.), ...)

But this does obviously not work. How would I solve that canonically with dplyr?

Upvotes: 9

Views: 1742

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101743

Here are some data.table options

setDT(d)[, u := which.max(unlist(.SD)), 1:nrow(d)]

or

setDT(d)[, u := max.col(.SD, "first")]

Upvotes: 2

akrun
akrun

Reputation: 887213

We just need the data to be specified as . as data.frame is a list with columns as list elements. If we wrap list(.), it becomes a nested list

library(dplyr)
d %>% 
  mutate(u = pmap_int(., ~ which.max(c(...))))
#  a b c u
#1 1 4 2 2
#2 2 3 3 2
#3 3 2 4 3
#4 4 1 5 3

Or can use cur_data()

d %>%
   mutate(u = pmap_int(cur_data(), ~ which.max(c(...))))

Or if we want to use everything(), place that inside select as list(everything()) doesn't address the data from which everything should be selected

d %>% 
   mutate(u = pmap_int(select(., everything()), ~ which.max(c(...))))

Or using rowwise

d %>%
    rowwise %>% 
    mutate(u = which.max(cur_data())) %>%
    ungroup
# A tibble: 4 x 4
#      a     b     c     u
#  <int> <int> <int> <int>
#1     1     4     2     2
#2     2     3     3     2
#3     3     2     4     3
#4     4     1     5     3

Or this is more efficient with max.col

max.col(d, 'first')
#[1] 2 2 3 3

Or with collapse

library(collapse)
dapply(d, which.max, MARGIN = 1)
#[1] 2 2 3 3

which can be included in dplyr as

d %>% 
    mutate(u = max.col(cur_data(), 'first'))

Upvotes: 7

Related Questions