Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find the closest row in data frame to my sample row in R?

I have data frame iris and my set of values my_row

structure(list(Sepal.Length = 4.65, Sepal.Width = 3.19, Petal.Length = 1.44, 
    Petal.Width = 0.3, Species = structure(1L, .Label = c("setosa", 
    "versicolor", "virginica"), class = "factor")), row.names = 1L, class = "data.frame")

> my_row
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1         4.65        3.19         1.44         0.3  setosa

> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

How to find closest row in iris data frame to my_row?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We may filter the rows of ‘iris’ where the ‘Species’ matches with the ‘Species’ from ‘my_row’, then get the absolute difference between the corresponding numeric columns of both datasets, get the rowSums of the difference, and slice the row with the minimum value in ‘new’ column

library(dplyr)
iris %>% 
   filter(Species == my_row$Species) %>% 
    mutate(new = rowSums(across(where(is.numeric), 
    ~ abs(.x - my_row[[cur_column()]])))) %>% 
    slice_min(n = 1, order_by = new)

-output

 Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
1          4.6         3.2          1.4         0.2  setosa 0.2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading