1
votes

First I have found the min and max value of a tibble:

library(dplyr)
name <- c("a","b")
x <- c(1,2)
y <- c(3,4)
df <- as_tibble(data.frame(name,x,y))
min_max <- df %>%
  select(-name) %>%
  summarize(min(.), max(.))

In this case min=1 and max=4

> min_max
# A tibble: 1 x 2
  `min(.)` `max(.)`
     <dbl>    <dbl>
1        1        4

How can I find the row and column names of min and max? The answer in this case should be ("a",x) and ("b",y). I tried which with no result.

1

1 Answers

2
votes

In base R, we can use which with arr.ind to get the row/column indices

which(df[-1] == min(df[-1]), arr.ind = TRUE)
which(df[-1] == max(df[-1]), arr.ind = TRUE)