Question

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...

Était-ce utile?

La solution 3

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))],]

Autres conseils

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]

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

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)}))))],]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top