【问题标题】:Complement of empty index vector is empty index vector空索引向量的补码是空索引向量
【发布时间】: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 中不需要加分号。
  • 我实际上注意到省略分号并不会产生错误。我想这是我必须改掉的习惯。谢谢。

标签: r indexing


【解决方案1】:

也许可以使用;

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
>

【讨论】:

  • 基本上 ttmaccer 是在说“你不需要使用哪个。你为什么使用哪个?”
  • 我用的是哪个,因为我的选择条件比较复杂。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
  • 2021-10-01
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多