Carolin V
Carolin V

Reputation: 51

How to calculate the minimum difference as a new column

I would like to know how to get the minimum difference of my columns per row (except id) into a new column. My dataset looks like this:

    id  1   2   3   4   5   6
1   1   39,00   23,00   39946,00    40556,00    41165,00    41772,00
2   2   43294,00    39034,00    39642,00    40250,00    40860,00    41468,00
3   3   42991,00    38730,00    39338,00    39946,00    40556,00    41165,00
4   4   42687,00    38424,00    39034,00    39642,00    NA  40860,00
5   5   42382,00    NA  38730,00    39338,00    39946,00    40556,00
6   6   42076,00    37815,00    38424,00    39034,00    39642,00    NA
7   7   41772,00    37512,00    38120,00    38730,00    39338,00    39946,00
8   8   41468,00    NA  37815,00    38424,00    39034,00    39642,00
9   9   41165,00    36904,00    37512,00    NA  41165,00    42991,00
10  10  40860,00    36598,00    37208,00    39034,00    40860,00    42687,00
11  11  40556,00    36293,00    36904,00    38730,00    40556,00    42382,00

Thank you for your suggestions!

structure(list(...1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), id = c(1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11), `1` = c(39, 43294, 42991, 42687, 
42382, 42076, 41772, 41468, 41165, 40860, 40556), `2` = c("23", 
"39034", "38730", "38424", "NA", "37815", "37512", "NA", "36904", 
"36598", "36293"), `3` = c(39946, 39642, 39338, 39034, 38730, 
38424, 38120, 37815, 37512, 37208, 36904), `4` = c("40556", "40250", 
"39946", "39642", "39338", "39034", "38730", "38424", "NA", "39034", 
"38730"), `5` = c("41165", "40860", "40556", "NA", "39946", "39642", 
"39338", "39034", "41165", "40860", "40556"), `6` = c("41772", 
"41468", "41165", "40860", "40556", "NA", "39946", "39642", "42991", 
"42687", "42382")), row.names = c(NA, -11L), class = c("tbl_df", 
"tbl", "data.frame"))

Upvotes: 1

Views: 72

Answers (2)

akrun
akrun

Reputation: 887148

An option with tidyverse

library(dplyr)
library(tidyr)
df1 %>%
   mutate(across(matches('^[0-9]'), as.numeric)) %>%
   pivot_longer(cols = matches('^[0-9]')) %>%
   group_by(id) %>% 
   summarise(min = min(diff(sort(value)))) %>%
   select(min) %>% 
   bind_cols(df1, .)

Upvotes: 2

user12728748
user12728748

Reputation: 8506

Your question is not clearly defined. If you mean absolute difference between any of the columns of each row, excluding NA values, then you can sort each row, calculate the difference between consecutive values and take the minimum of that:

df$min <- apply(as.matrix(df[,-1]), 1, function(x) min(diff(sort(x))))

Upvotes: 2

Related Questions