Home > database >  r function similar to pmin() but finds nth lowest value across columns in dataframe?
r function similar to pmin() but finds nth lowest value across columns in dataframe?

Time:07-01

I would like a function that would find the nth lowest value across columns. In other words, a function that is similar to pmin() but rather than finding the lowest, I am hoping it returns the nth lowest. Thank you in advance!

CodePudding user response:

df %>%
  rowid_to_column() %>%
  pivot_longer(-rowid)%>%
  arrange(value)%>% #You could arrange with decreasing to find max
  group_by(rowid) %>%
  summarise(value = nth(value, 2)) # Find the second minimum

# A tibble: 10 x 2
   rowid   value
   <int>   <dbl>
 1     1 -0.560 
 2     2 -0.218 
 3     3  0.401 
 4     4  0.0705
 5     5 -0.556 
 6     6  1.72  
 7     7  0.498 
 8     8 -1.27  
 9     9 -0.687 
10    10 -0.446 

CodePudding user response:

Here is a simple one (it could be modified to deal with NAs):

nth_lowest <- function(x,n) x[order(x)[n]]

Apply it to a data frame, using rowwise() and c_across() from the dplyr package.

df %>%
  rowwise() %>%
  mutate( second_lowest = f(c_across(x:z),2))

Output:

         x      y      z second_lowest
     <dbl>  <dbl>  <dbl>         <dbl>
 1 -0.560   1.22  -1.07        -0.560 
 2 -0.230   0.360 -0.218       -0.218 
 3  1.56    0.401 -1.03         0.401 
 4  0.0705  0.111 -0.729        0.0705
 5  0.129  -0.556 -0.625       -0.556 
 6  1.72    1.79  -1.69         1.72  
 7  0.461   0.498  0.838        0.498 
 8 -1.27   -1.97   0.153       -1.27  
 9 -0.687   0.701 -1.14        -0.687 
10 -0.446  -0.473  1.25        -0.446 

Input:

set.seed(123)
df <- data.frame(x=rnorm(10), y=rnorm(10), z=rnorm(10))

CodePudding user response:

We may also do this with pmap and nth

library(purrr)
library(dplyr)
pmap_dbl(df, ~ nth(sort(c(...)), n = 2))
  •  Tags:  
  • r
  • Related