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

Updated on: 21-Aug-2020

363 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements