使用通配符子集数字数据?
Subset numeric data using wildcards?
我需要根据站点 ID 过滤数据集。
本质上,我想 select 所有以 01 结尾的站点 ID。
站点 ID 的长度为 6 位。例如:
myData <- data.frame(ID = c(202001, 202002, 202003, 203001), someData = c(10, 20, 30, 40))
我可以在数字(或整数)数据上使用通配符吗,例如:
filter(myData, ID = ****01)
您可以将 grepl()
(和其他正则表达式匹配函数)与正则表达式 01$
一起使用。 $
表示我们希望匹配从字符串的末尾开始。
myData[grepl("01$", myData$ID), ]
# ID someData
# 1 202001 10
# 4 203001 40
@thelatemail 在评论中有一个 dplyr 方法,也使用 grepl()
.
filter(myData, grepl("01$", ID))
说到给猫剥皮的方法
filter(myData, substr(ID, 5, 7) == "01")
# ID someData
# 1 202001 10
# 2 203001 40
我需要根据站点 ID 过滤数据集。 本质上,我想 select 所有以 01 结尾的站点 ID。 站点 ID 的长度为 6 位。例如:
myData <- data.frame(ID = c(202001, 202002, 202003, 203001), someData = c(10, 20, 30, 40))
我可以在数字(或整数)数据上使用通配符吗,例如:
filter(myData, ID = ****01)
您可以将 grepl()
(和其他正则表达式匹配函数)与正则表达式 01$
一起使用。 $
表示我们希望匹配从字符串的末尾开始。
myData[grepl("01$", myData$ID), ]
# ID someData
# 1 202001 10
# 4 203001 40
@thelatemail 在评论中有一个 dplyr 方法,也使用 grepl()
.
filter(myData, grepl("01$", ID))
说到给猫剥皮的方法
filter(myData, substr(ID, 5, 7) == "01")
# ID someData
# 1 202001 10
# 2 203001 40