[英]R subset logical
我有一個數據列表,我想根據兩個變量(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 條目進行子集化?
謝謝你。
您混合了基於索引的子集(使用整數)和包含-排除子集(使用布爾值)。
> x <- 9:12
> grep('1', x) # indexes
[1] 2 3 4
> x[grep('1', x)]
[1] 10 11 12
> grepl('1', x) # boolean
[1] FALSE TRUE TRUE TRUE
> x[grepl('1', x)]
[1] 10 11 12
> x<11 # boolean
[1] TRUE TRUE FALSE FALSE
> x[x<11]
[1] 9 10
> which(x<11) # indexes
[1] 1 2
> x[which(x<11)]
[1] 9 10
您只能將索引與索引組合或將布爾值與布爾值組合。
> grepl('1',x) & x<11 # both boolean
[1] FALSE TRUE FALSE FALSE
> intersect(grep('1',x), which(x<11)) # both indexes
[1] 2
> grep('1',x) & x<11 # mixed
[1] TRUE TRUE FALSE FALSE
Warning message:
In grep("1", x) & x < 11 :
longer object length is not a multiple of shorter object length
我喜歡dplyr
包中的filter
功能
library(dplyr)
filter(x, grepl("2014-10",Deployment.Date) & Partition==" TermChange"))
(在本地數據上測試)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.