- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract the closest value to a certain value in each category in an R data frame?
In Data Analysis, we often deal with the comparison of values and this comparison could be also done after finding the closest value to a certain value that might be threshold. For this purpose, we can use filter function of dplyr package along with abs and min function, the abs and min function are required to create the formula for finding the closest value.
Consider the below data frame −
Example
Category<-sample(LETTERS[1:3],20,replace=TRUE) Y<-rpois(20,2) df1<-data.frame(Category,Y) df1
Output
Category Y 1 C 2 2 B 1 3 C 1 4 B 3 5 B 0 6 C 1 7 C 3 8 A 3 9 B 3 10 C 1 11 A 0 12 B 2 13 A 2 14 B 3 15 B 0 16 B 5 17 B 4 18 C 4 19 B 2 20 A 0
Loading dplyr package and finding the closest value to 5 in Y column for each Category −
Example
library(dplyr) df1%>%group_by(Category)%>%filter(abs(Y-5)==min(abs(Y-5))) # A tibble: 3 x 2 # Groups: Category [3]
Output
Category Y <chr> <int> 1 A 3 2 B 5 3 C 4
Example
Class<-sample(c("I","II","III"),20,replace=TRUE) Y<-rnorm(20) df2<-data.frame(Class,Y) df2
Output
Class Y 1 III 0.8489651 2 I 0.1588493 3 II -0.4598459 4 I 0.5460653 5 I -1.2462101 6 II -1.1553006 7 III 0.1208797 8 I -0.8272769 9 III -0.1296539 10 III 0.4404777 11 I -1.4512669 12 I -0.1486225 13 II 0.8484109 14 II 0.9099450 15 I 0.6118836 16 III -1.5100937 17 II -2.0682287 18 III 0.1454986 19 III -1.9322351 20 I 0.8039917
Finding the closest value to 0.2 in Y column for each Class −
Example
df2%>%group_by(Class)%>%filter(abs(Y-0.2)==min(abs(Y-0.2))) # A tibble: 3 x 2 # Groups: Class [3]
Output
Class Y <chr> <dbl> 1 I 0.159 2 II 0.848 3 III 0.145
- Related Articles
- How to extract the first highest occurring value in an R data frame column?
- How to subset an R data frame if numerical column is greater than a certain value for a particular category in grouping column?
- How to divide each value in an R data frame by 100?
- How to find the frequency of each value in an R data frame?
- How to find the percentage of each category in an R data frame column?
- How to find the count of each category in an R data frame column?
- How to extract a data frame’s column value based on a column value of another data frame in R?
- How to extract a particular value based on index from an R data frame column?
- How to change a text value in an R data frame?
- How to create a data frame of the maximum value for each group in an R data frame using dplyr?
- How to find the maximum value in an R data frame?
- How to update single value in an R data frame?
- How to add a string before each numeric value in an R data frame column?
- How to add a new column to an R data frame with largest value in each row?
- How to divide each value in a data frame by column total in R?

Advertisements