使用多个条件对 data.frame 进行子集化

发布于 2024-09-05 20:05:34 字数 720 浏览 1 评论 0原文

假设我的数据如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半﹌身腐败 2024-09-12 20:05:34

对于这个问题,我会采用学徒队列的答案中的方法,即从日期中提取年份,而不是进行通用字符串匹配。我建议:

data[data$Analyte =="ATRAZINE"
     & as.POSIXlt(data$Date, format="%m/%d/%Y")$year == 106]

但如果您确实必须进行正则表达式匹配,则可以使用返回逻辑向量的 grepl 而不是返回索引向量的 grep

data[data$Analyte=="ATRAZINE" & grepl("2006",as.character(data$Date)),]

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:

data[data$Analyte =="ATRAZINE"
     & as.POSIXlt(data$Date, format="%m/%d/%Y")$year == 106]

But if you really had to do regexp matching, you could use grepl which returns a logical vector rather than grep which returns a vector of indices.

data[data$Analyte=="ATRAZINE" & grepl("2006",as.character(data$Date)),]
亽野灬性zι浪 2024-09-12 20:05:34

使用日期文字的一种方法:

data[data$Analyte =="ATRAZINE"
     & (data$Date >= '2006-01-01' & data$Date < '2007-01-01')]

使用 format 的另一种方法

data[data$Analyte =="ATRAZINE"
     & format(data$Date, "%Y") == '2006']

One way using date literals:

data[data$Analyte =="ATRAZINE"
     & (data$Date >= '2006-01-01' & data$Date < '2007-01-01')]

Another way using format

data[data$Analyte =="ATRAZINE"
     & format(data$Date, "%Y") == '2006']
抱猫软卧 2024-09-12 20:05:34

意识到这个问题已经在很多年前被问过,希望将来能对某些人有所帮助。

使用dplyr进行多个条件的子设置,并检查转换为Date类型后的年份

library(dplyr)

data %>% filter( Analyte=="ATRAZINE" & format(as.Date(Date,format = "%m/%d/%Y"),"%Y") == "2006") 

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

library(dplyr)

data %>% filter( Analyte=="ATRAZINE" & format(as.Date(Date,format = "%m/%d/%Y"),"%Y") == "2006") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文