gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:30:15 25 4
gpt4 key购买 nike

R 用户,

我有这个数据框:

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

我只需要获取 2002 年的值。所以,我应该在列 MM.GG.AA 上选择那些以“/02”结尾的行。我没有在网上找到任何东西......任何提示?谢谢!

最佳答案

使用带有 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

grep 正则表达式 /02$ 搜索以 /02 结尾的字符串,因为 $表示字符串的结尾。

关于string - 如何通过R中字符串的最后一个字符对数据帧进行子集化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16301059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com