使用多个条件对 data.frame 进行子集化
假设我的数据如下所示:
2372 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 1.3 05/07/2006
9104 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 0.34 07/23/2006
9212 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 0.33 02/11/2007
2094 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 1.4 05/06/2007
16763 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 0.61 05/11/2009
1076 Kansas KS2000111 HUMBOLDT, CITY OF METOLACHLOR 0.48 05/12/2002
1077 Kansas KS2000111 HUMBOLDT, CITY OF METOLACHLOR 0.3 05/07/2006
我希望能够按分析物和日期的部分匹配进行子集化(即我只想要年份)。我一直在尝试这样做,但我知道这不太正确。
data[data$Analyte=="ATRAZINE" & grep("2006",as.character(data$Date)),]
有什么建议吗?
Suppose my data looks like this:
2372 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 1.3 05/07/2006
9104 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 0.34 07/23/2006
9212 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 0.33 02/11/2007
2094 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 1.4 05/06/2007
16763 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 0.61 05/11/2009
1076 Kansas KS2000111 HUMBOLDT, CITY OF METOLACHLOR 0.48 05/12/2002
1077 Kansas KS2000111 HUMBOLDT, CITY OF METOLACHLOR 0.3 05/07/2006
I want to be able to subset by the Analyte and a partial match on the date(namely I just want the year). I have been trying this, but I know it isn't quite right.
data[data$Analyte=="ATRAZINE" & grep("2006",as.character(data$Date)),]
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于这个问题,我会采用学徒队列的答案中的方法,即从日期中提取年份,而不是进行通用字符串匹配。我建议:
但如果您确实必须进行正则表达式匹配,则可以使用返回逻辑向量的
grepl
而不是返回索引向量的grep
。For this problem I would go with the approach in Apprentice Queue's answer of extracting the year from the date rather than doing generic string matching. I would suggest:
But if you really had to do regexp matching, you could use
grepl
which returns a logical vector rather thangrep
which returns a vector of indices.使用日期文字的一种方法:
使用
format
的另一种方法One way using date literals:
Another way using
format
意识到这个问题已经在很多年前被问过,希望将来能对某些人有所帮助。
使用dplyr进行多个条件的子设置,并检查转换为Date类型后的年份
Realize this question has been asked quite some years back, hopefully should help some one in the future.
Used dplyr for sub-setting using multiple conditions, and checking the year after converting into Date type