Search code examples
rlogical-operatorssubset

R subset logical


I have a list of data that I want to subset based on two variables (partition, deployment.date). Based on the articles and manuals, I should be able to do it using a single ampersand. What I am seeing however is that each one works on its own but not when combined.

> 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

But when I use them together the result isn't what I expect.

> 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

I have played with the parenthesis groupings as well as using double-ampersands. What am I missing so that I can subset all TermChange entries deployed on 2014-10?

Thank you.


Solution

  • I like the filter function from the dplyr package

    library(dplyr)
    filter(x, grepl("2014-10",Deployment.Date) & Partition==" TermChange"))
    

    (tested on local data)