the below example is to obtain the min value amoing 3 columns, we can use the pmin(V1, V2, V3)
if we have lots of column, how to get the minimu value among all numeric variables especially using the tidyverse
<code>df <- data.frame(V1= runif(3, 1, 10), V2 =runif(3, 1, 10), V3 = runif(3, 1, 10))
df |> dplyr::mutate(min = pmin(V1, V2, V3))
#> V1 V2 V3 min
#> 1 9.416508 7.433805 8.318565 7.433805
#> 2 9.194577 5.935652 4.486447 4.486447
#> 3 2.537344 9.764215 1.473823 1.473823
</code>
<code>df <- data.frame(V1= runif(3, 1, 10), V2 =runif(3, 1, 10), V3 = runif(3, 1, 10))
df |> dplyr::mutate(min = pmin(V1, V2, V3))
#> V1 V2 V3 min
#> 1 9.416508 7.433805 8.318565 7.433805
#> 2 9.194577 5.935652 4.486447 4.486447
#> 3 2.537344 9.764215 1.473823 1.473823
</code>
df <- data.frame(V1= runif(3, 1, 10), V2 =runif(3, 1, 10), V3 = runif(3, 1, 10))
df |> dplyr::mutate(min = pmin(V1, V2, V3))
#> V1 V2 V3 min
#> 1 9.416508 7.433805 8.318565 7.433805
#> 2 9.194577 5.935652 4.486447 4.486447
#> 3 2.537344 9.764215 1.473823 1.473823
Created on 2024-06-28 with reprex v2.1.0
Automatically get all numeric columns and passed to a functione, e.g. pmin
I try the following function dplyr::pick
and dplyr::where
but fail to get right results
<code>df <- data.frame(V1= runif(3, 1, 10), V2 =runif(3, 1, 10), V3 = runif(3, 1, 10))
df |> dplyr::mutate(x= pmin(dplyr::pick(dplyr::where(is.numeric))))
</code>
<code>df <- data.frame(V1= runif(3, 1, 10), V2 =runif(3, 1, 10), V3 = runif(3, 1, 10))
df |> dplyr::mutate(x= pmin(dplyr::pick(dplyr::where(is.numeric))))
</code>
df <- data.frame(V1= runif(3, 1, 10), V2 =runif(3, 1, 10), V3 = runif(3, 1, 10))
df |> dplyr::mutate(x= pmin(dplyr::pick(dplyr::where(is.numeric))))