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

▼魔方 西西 提交于 2019-12-21 04:31:36

问题


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)

回答1:


Another possible solution

 setdiff(full.list,data.list)



回答2:


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



回答3:


Another option using match (similar to %in%)

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



回答4:


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



来源:https://stackoverflow.com/questions/17263936/identify-missing-values-in-a-sequence-perform-asymmetric-difference-between-tw

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!