Complement of empty index vector is empty index vector

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

I am removing values from a vector by using - (minus sign) in front of the index vector. Like this:

scores <- scores[-indexes.to.delete]

Sometimes indexes.to.delete vector is empty, that is N/A. So the scores vector should then remain unchanged. However, I am getting empty scores vector when indexes.to.delete is empty.

Example:

x <- c(1, 2, 3); y <- c(4, 5, 6); indexes.to.delete <- which(y < x); # will return empty vector y <- y[-indexes.to.delete]; # returns empty y vector, but I want y stay untouched 

I could code an if statement checking whether indexes.to.delete is empty, but I am wondering if there is a simpler way?

回答1:

Maybe use;

x <- c(1, 2, 3) y <- c(4, 5, 6) y[!y<x] > y[!y<x] [1] 4 5 6  x <- c(1, 2, 3) y <- c(4, 1, 6) > y[!y<x] [1] 4 6 > 


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