Reputation: 1878
my dataset looks like following:
ID Score
A1 60
A1 50
A1 NA
B1 30
B1 33
C1 48
C1 39
D1 21
D1 38
D1 NA
I would like to see duplicated records which has NA's. Such as:
A1 60
A1 50
A1 NA
D1 21
D1 38
D1 NA
Thanks for your time and kind consideration...
Upvotes: 1
Views: 73
Reputation: 5167
you can try this
mydata<-data.frame(ID=c(rep("A1",3),rep("B1",2),rep("C1",2),rep("D1",3)),Score=c(60,50,NA,30,33,48,39,21,38,NA))
mydata[mydata$ID%in%unique(mydata$ID)[-which(is.na(as.vector(tapply(mydata$Score,mydata$ID,FUN=function(x){match(NA,x)}))))],]
Upvotes: 0
Reputation: 42639
An approach using ave
. Compose
thrown in for fun:
require(functional)
DF[as.logical(ave(DF$Score, DF$ID, FUN=Compose(is.na, any))),]
## ID Score
## 1 A1 60
## 2 A1 50
## 3 A1 NA
## 8 D1 21
## 9 D1 38
## 10 D1 NA
Upvotes: 4
Reputation: 115392
A couple of approaches using data.table.
Assuming your data is in a data.frame called DF
library(data.table)
DT <- data.table(DF, key = 'ID')
# self join with the ID values with NA values in score
DT[.(DT[is.na(Score),unique(ID)])]
# or
DT[,if(any(is.na(Score))) {.SD},by=ID]
Upvotes: 4
Reputation: 16026
There might be a slightly neater way to do this:
df <- data.frame(ID=rep(c("A1", "B1", "C1"), each=4), Score=sample(1:100,12))
df$Score[c(1,7)] <- NA
df[df$ID %in% df$ID[which(is.na(df$Score))],]
Upvotes: 3