如何根据R中列的行值的子字符串选择data.table?
我们经常在 R 中创建数据子集,以根据数据分析项目中整个目标的较小目标进行计算。有时,此子集以字符串而不是数值为条件。我们还可以data.table使用 grep 函数基于列的行值的子字符串创建一个子集。
示例
考虑以下data.table对象 -
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
x1 S.No1: 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
子集可以按如下所示完成 -
示例
IndianCities[grep("il",x1)]输出结果x1 S.No
1: Bareilly 12
IndianCities[grep("ac",x1)]
x1 S.No
1: Cuttack 13
IndianCities[grep("a",x1)]
x1 S.No1: 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
以上是 如何根据R中列的行值的子字符串选择data.table? 的全部内容, 来源链接: utcz.com/z/311478.html