Dr. Beeblebrox
Dr. Beeblebrox

Reputation: 848

Identify missing values in a sequence / perform asymmetric difference between two lists

Using R, I want to efficiently identify which values in a sequence are missing. I've written the below example of how I do it. There must be a better way. Can someone help?

data.list=c(1,2,4,5,7,8,9)

full.list=seq(from = 1, to = 10, by =1)

output <- c()
for(i in 1:length(full.list)){
    holder1 <- as.numeric(any(data.list == i))
    output[i] <- holder1
}

which(output == 0)

Upvotes: 7

Views: 4460

Answers (4)

seasmith
seasmith

Reputation: 919

Using grep():

grep(paste("^", data.list, "$", sep = "", collapse = "|"), full.list, invert = TRUE)

You could be "lazy" and use collapse = ^|$ but use the above for precise accuracy.

Using grepl():

full.list[!grepl(paste("^", data.list, "$", sep = "", collapse = "|"), full.list)]

Upvotes: 0

agstudy
agstudy

Reputation: 121578

Another option using match (similar to %in%)

full.list[!match(full.list,data.list,nomatch=FALSE)]
[1]  3  6 10

Upvotes: 2

Geoffrey Absalom
Geoffrey Absalom

Reputation: 1895

Another possible solution

 setdiff(full.list,data.list)

Upvotes: 13

Hong Ooi
Hong Ooi

Reputation: 57686

full.list[!full.list %in% data.list]

Upvotes: 3

Related Questions