- 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 select of data.table based on substring of row values for a column in R?
We often create subsets of data in R to perform calculations based on smaller objectives of a whole objective in data analysis projects. Sometimes this subsetting is conditional on strings instead of numeric values. We can also create a subset of data.table on the basis of substring of row values for a column by using grep function.
Example
Consider the below data.table object −
x1<-c("Lucknow","Kanpur","Chennai","Delhi","Mumbai","Jammu","Jodhpur","Jaipur","Ludhiana","Dehradun","Aligarh","Bareilly","Cuttack","Sonipat","Gurgaon","Noida","Bengluru","Kolkata","Ahmedabad","Patna") S.No<-1:20 IndianCities<-data.table(x1,S.No) IndianCities
Output
x1 S.No 1: Lucknow 1 2: Kanpur 2 3: Chennai 3 4: Delhi 4 5: Mumbai 5 6: Jammu 6 7: Jodhpur 7 8: Jaipur 8 9: Ludhiana 9 10: Dehradun 10 11: Aligarh 11 12: Bareilly 12 13: Cuttack 13 14: Sonipat 14 15: Gurgaon 15 16: Noida 16 17: Bengluru 17 18: Kolkata 18 19: Ahmedabad 19 20: Patna 20
Subsetting can be done as shown below −
Example
IndianCities[grep("il",x1)] x1 S.No 1: Bareilly 12 IndianCities[grep("ac",x1)] x1 S.No 1: Cuttack 13 IndianCities[grep("a",x1)]
Output
x1 S.No 1: Kanpur 2 2: Chennai 3 3: Mumbai 5 4: Jammu 6 5: Jaipur 8 6: Ludhiana 9 7: Dehradun 10 8: Aligarh 11 9: Bareilly 12 10: Cuttack 13 11: Sonipat 14 12: Gurgaon 15 13: Noida 16 14: Kolkata 18 15: Ahmedabad 19 16: Patna 20 IndianCities[grep("b",x1)] x1 S.No 1: Mumbai 5 2: Ahmedabad 19 IndianCities[grep("c",x1)] x1 S.No 1: Lucknow 1 2: Cuttack 13 IndianCities[grep("g",x1)] x1 S.No 1: Aligarh 11 2: Gurgaon 15 3: Bengluru 17 IndianCities[grep("at",x1)] x1 S.No 1: Sonipat 14 2: Kolkata 18 3: Patna 20 IndianCities[grep("o",x1)] x1 S.No 1: Lucknow 1 2: Jodhpur 7 3: Sonipat 14 4: Gurgaon 15 5: Noida 16 6: Kolkata 18 IndianCities[grep("u",x1)] x1 S.No 1: Lucknow 1 2: Kanpur 2 3: Mumbai 5 4: Jammu 6 5: Jodhpur 7 6: Jaipur 8 7: Ludhiana 9 8: Dehradun 10 9: Cuttack 13 10: Gurgaon 15 11: Bengluru 17
- Related Articles
- How to divide row values of a numerical column based on categorical column values in an R data frame?
- How to change row values based on column values in an R data frame?
- How to select rows based on range of values of a column in an R data frame?
- How to select data.table object columns based on their class in R?
- How to randomize column values of a data.table object for all columns in R?
- How to randomize column values of a data.table object for a set of columns in R?
- How to multiply corresponding row values in a data.table object with single row data.table object in R?
- How to repeat column values of a data.table object in R by number of values in another column?
- How to select the first and last row based on group column in an R data frame?
- How to fill a data.table row with missing values in R?
- How to extract the row for groupwise maximum in another column of an R data.table object?
- How to get row index based on a value of an R data frame column?
- R Programming to find the column name for row maximum in a data.table object.
- How to find the number of unique values for each column in data.table object in R?
- Find the sum of a column values based on another numerical column in R.

Advertisements