我有一个数据列表,我希望根据两个变量(partition,deployment.date)进行子集化。根据文章和手册,我应该能够使用一个&符号来做到这一点。然而,我所看到的是,每一个都是独立工作,但不是在合并时。
> tail(x) Composite Version Partition Deployment.Date 6 MaintenanceService 1.4.34.5 SpecialProgram-IneligibleCR 2014-10-01 7 MaintenanceService 1.4.34.5 SpecialProgram-IneligibleCR 2014-10-01 8 Integration 1.6.1 SpecialProgram-PT 2014-10-13 9 Integration 1.6.1 SpecialProgram-PT 2014-10-13 10 UpdateTermChanges 1.9.0 TermChange 2014-09-28 11 UpdateTermChangesV2 1.13.0.1 TermChange 2014-09-24 > x[ grep("2014-10", x$Deployment.Date) , ] Composite Version Partition Deployment.Date 1 TermChangeEventProcessor 1.9.1.1 TermChange 2014-10-31 2 TermChangeIntegrationLayer 1.1.2.1 TermChange 2014-10-31 3 UpdateOffers 2.5.2 TermChange 2014-10-10 4 UpdateOffers 2.5.3 TermChange 2014-10-13 5 MaintenanceService 1.4.34.4 SpecialProgram-IneligibleCR 2014-10-01 6 MaintenanceService 1.4.34.5 SpecialProgram-IneligibleCR 2014-10-01 7 MaintenanceService 1.4.34.5 SpecialProgram-IneligibleCR 2014-10-01 8 Integration 1.6.1 SpecialProgram-PT 2014-10-13 9 Integration 1.6.1 SpecialProgram-PT 2014-10-13 > x[(x$Partition == " TermChange"), ] Composite Version Partition Deployment.Date 1 TermChangeEventProcessor 1.9.1.1 TermChange 2014-10-31 2 TermChangeIntegrationLayer 1.1.2.1 TermChange 2014-10-31 3 UpdateOffers 2.5.2 TermChange 2014-10-10 4 UpdateOffers 2.5.3 TermChange 2014-10-13 10 UpdateTermChanges 1.9.0 TermChange 2014-09-28 11 UpdateTermChangesV2 1.13.0.1 TermChange 2014-09-24
但是当我一起使用它们时,结果并不是我所期望的。
> x[( (grep("2014-10", x$Deployment.Date)) & (x$Partition == " TermChange")), ] Composite Version Partition Deployment.Date 1 TermChangeEventProcessor 1.9.1.1 TermChange 2014-10-31 2 TermChangeIntegrationLayer 1.1.2.1 TermChange 2014-10-31 3 UpdateOffers 2.5.2 TermChange 2014-10-10 4 UpdateOffers 2.5.3 TermChange 2014-10-13 10 UpdateTermChanges 1.9.0 TermChange 2014-09-28 11 UpdateTermChangesV2 1.13.0.1 TermChange 2014-09-24 Warning message: In (grep("2014-10", x$Deployment.Date)) & (x$Partition == " TermChange") : longer object length is not a multiple of shorter object length
我玩过括号分组以及使用双&符号。我缺少什么,以便我可以对2014-10中部署的所有TermChange条目进行分组?
谢谢。
答案 0 :(得分:1)
您已混合整数子集和逻辑子集。
> x <- 9:12
> grep('1', x)
[1] 2 3 4
> x<11
[1] TRUE TRUE FALSE FALSE
> grep('1',x) & x<11
[1] TRUE TRUE FALSE FALSE
Warning message:
In grep("1", x) & x < 11 :
longer object length is not a multiple of shorter object length
> which(x<11)
[1] 1 2
> intersect(grep('1',x), which(x<11))
[1] 2
> grepl('1',x) & x<11
[1] FALSE TRUE FALSE FALSE
答案 1 :(得分:0)
我喜欢filter
包
dplyr
函数
library(dplyr)
filter(x, grepl("2014-10",Deployment.Date) & Partition==" TermChange"))
(在本地数据上测试)