
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Percentage of Values Within a Range in R Data Table
To find the percentage of values that lie within a range in column of a data.table object in R, we can follow the below steps −
First of all, create a data.table object.
Then, use sum function along with extreme values for range and length function to find the percentage of values that lie within that range.
Example
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table) x<-rnorm(30) DT<-data.table(x) DT
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x 1: -0.01820109 2: 0.09807795 3: 0.87645348 4: 0.22873684 5: 0.48448009 6: 0.13104149 7: -0.58816453 8: -0.06087622 9: 0.19032472 10: -0.39708670 11: 0.56044501 12: -1.42750377 13: -0.76757968 14: -0.85976410 15: -0.45661539 16: -0.12494892 17: -0.02593251 18: 0.64340403 19: 0.66718194 20: -0.93805129 21: -0.27078554 22: 0.91980427 23: 1.48243157 24: -0.52458819 25: 0.73368864 26: 0.11948186 27: -0.67115652 28: -0.70762527 29: -0.26249638 30: -0.22123573 x
Find the percentage of values that lie within a range
Using sum function along with the range that is 0.5 and 0.9 and length function to find the percentage of values that lie within this range −
library(data.table) x<-rnorm(30) DT<-data.table(x) sum(DT$x>0.5 & DT$x<0.9)/length(DT$x)
Output
[1] 0.1666667
Advertisements