Home > database >  How to extract the minimum value in all columns in a dataframe in R?
How to extract the minimum value in all columns in a dataframe in R?

Time:07-14

I am working on mass spectrometry proteomics expression data. For statistical analysis of the data, I have to find the top three minimum value of each column in the dataframe like below,

structure(list(Type = c("knn_vsn", "knn_loess", "knn_rlr", "lls_vsn", 
"lls_loess", "lls_rlr", "svd_vsn", "svd_loess", "svd_rlr"), Group1 = c(0.00318368971435714, 
0.00317086486813191, 0.00317086486813191, 0.00312821095645019, 
0.00311632537571597, 0.00313568333628438, 0.00394831935666465, 
0.00393605637633005, 0.00395599132474446), Group2 = c(0.0056588221783197, 
0.00560933517836751, 0.00560933517836751, 0.00550114679857588, 
0.00548316209864631, 0.00550230673346083, 0.00737865310351839, 
0.0073411154394253, 0.00735748595511963), Group3 = c(0.00418838138878096, 
0.00417201215938804, 0.00417201215938804, 0.00398819978362592, 
0.00397093259462351, 0.00398827962107259, 0.00424157479553304, 
0.00422638750183658, 0.00424175886713471), Group4 = c(0.0039811913527127, 
0.00394649435912413, 0.00394649435912413, 0.00397059873107098, 
0.00393840233766712, 0.00396385071387178, 0.0041077267588457, 
0.00407577176849463, 0.00410191492380459)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -9L), groups = structure(list(
    Type = c("knn_loess", "knn_rlr", "knn_vsn", "lls_loess", 
    "lls_rlr", "lls_vsn", "svd_loess", "svd_rlr", "svd_vsn"), 
    .rows = structure(list(2L, 3L, 1L, 5L, 6L, 4L, 8L, 9L, 7L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -9L), .drop = TRUE))

And I need the output like below,\

structure(list(`Type ` = c("lls_loess", "lls_rlr", "lls_vsn"), 
    Group1 = c(0.00311632537571597, 0.00313568333628438, 0.00312821095645019
    ), ` Type` = c("lls_loess", "lls_rlr", "lls_vsn"), Group2 = c(0.00548316209864631, 
    0.00550230673346083, 0.00550114679857588), `  Type` = c("lls_loess", 
    "lls_rlr", "lls_vsn"), Group3 = c(0.00397093259462351, 0.00398827962107259, 
    0.00398819978362592), `Type  ` = c("lls_loess", "lls_rlr", 
    "lls_vsn"), Group4 = c(0.00393840233766712, 0.00396385071387178, 
    0.00397059873107098)), class = "data.frame", row.names = c(NA, 
-3L))

Please suggest some useful R code for this issue. Thank you in advance.

CodePudding user response:

library(tidyverse)
df %>%
  pivot_longer(-Type) %>%
  group_by(name) %>%
  slice_min(value, n = 3) %>%      # You might stop here, already tidy
  mutate(row = row_number()) %>%
  ungroup() %>%
  pivot_wider(names_from = name, values_from = c(Type, value),
              names_vary = "slowest")

Result

# A tibble: 3 × 9
    row Type_Group1 value_Group1 Type_Group2 value_Group2 Type_Group3 value_Group3 Type_Group4 value_Group4
  <int> <chr>              <dbl> <chr>              <dbl> <chr>              <dbl> <chr>              <dbl>
1     1 lls_loess        0.00312 lls_loess        0.00548 lls_loess        0.00397 lls_loess        0.00394
2     2 lls_vsn          0.00313 lls_vsn          0.00550 lls_vsn          0.00399 knn_loess        0.00395
3     3 lls_rlr          0.00314 lls_rlr          0.00550 lls_rlr          0.00399 knn_rlr          0.00395

CodePudding user response:

Take a look at this

data <- data.frame(group = rep(letters[1:3], each = 5),data value = 1:3) data

  • Related