Suppose I have a tibble such as
t <- tibble(
x = c("a", "b", "c"),
y = c("d", "e", "f"),
z = c("g", "h", "i")
)
and I would like to find the minimum value over all values in all columns. What's the best way to do this within the Tidyverse mindset?
If I try min(t)
, I get the error
Error in FUN(X[[i]], ...) :
only defined on a data frame with all numeric-alike variables
The only ways I've found to do this so far involve extracting out the individual vectors and finding the min over those. For example, t_min <- min(c(t$x, t$y, t$z))
gives "a"
. This works, but it doesn't extend well to many columns.
Note: assume that the "min" function gives the desired result when applied to the string data.
CodePudding user response:
With tidyverse
/dplyr
you can
library(tidyverse)
t %>%
summarise(across(everything(), min))
# A tibble: 1 × 3
x y z
<chr> <chr> <chr>
1 a d g
Minimum of the dataset
t %>%
summarise(across(everything(), min)) %>%
rowwise() %>%
mutate(min = min(c_across(x:z)))
CodePudding user response:
Here's how to get the min value from each column (thanks @benson for the clean implementation):
sapply(t, min)
# x y z
# "a" "d" "g"
And how to get the min value from an entire tibble (thanks to @m-- for pointing out that it's not necessary to unname()
the vector before taking the min()
):
min(unlist(t))
# [1] "a"
CodePudding user response:
Another possible solution, based on apply
:
library(tibble)
apply(t, 2, min)
#> x y z
#> "a" "d" "g"