简体   繁体   English

如何通过R中字符串的最后一个字符对数据帧进行子集化

[英]How to subset dataframe by last characters of a string in R

R-user, R-用户,

I have this dataframe: 我有这个数据帧:

head(Niger_Meteo_98.11)
  X.ID_punto    MM.GG.AA T2m_max  
1          1    01/01/98 303.235 
2          2    01/01/99 303.356 
3          3    01/01/00 303.477 
4          4    01/01/01 303.604 
5          5    01/01/02 303.759 
6          6    01/01/03 303.915 

and I need to get only values from year 2002. So, I should select, on column MM.GG.AA , those rows that end with " /02 ". 我只需要从2002年获得值。因此,我应该在MM.GG.AA列中选择那些以“ /02 ”结尾的行。 I didn't find anything online...any hints? 我没有在网上找到任何东西......任何提示? Thanks! 谢谢!

Use standard subsetting with grep , like this: 使用grep标准子集,如下所示:

x <- read.table(text="  X.ID_punto    MM.GG.AA T2m_max  
1          1    01/01/98 303.235 
2          2    01/01/99 303.356 
3          3    01/01/00 303.477 
4          4    01/01/01 303.604 
5          5    01/01/02 303.759 
6          6    01/01/03 303.915", header=TRUE)

x[grep("/02$", x$MM.GG.AA), ]

  X.ID_punto MM.GG.AA T2m_max
5          5 01/01/02 303.759

The grep regular expression /02$ searches for string that end in /02 , since the $ indicates the end of the string. grep正则表达式/02$搜索以/02结尾的字符串,因为$表示字符串的结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM