【发布时间】:2012-08-11 15:14:59
【问题描述】:
我通过在索引向量前面使用 -(减号)从向量中删除值。像这样:
scores <- scores[-indexes.to.delete]
有时indexes.to.delete 向量为空,即 N/A。所以scores 向量应该保持不变。但是,当indexes.to.delete 为空时,scores 向量为空。
例子:
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
我可以编写一个 if 语句来检查 indexes.to.delete 是否为空,但我想知道是否有更简单的方法?
【问题讨论】:
-
R 中不需要加分号。
-
我实际上注意到省略分号并不会产生错误。我想这是我必须改掉的习惯。谢谢。