簡體   English   中英

從向量/矩陣或 R 中的 Dataframe 中查找索引

[英]Finding Index from Vector/Matrix or Dataframe in R

我在 R 中有如下數據:

data <- c(1,12,22,0,8,1,0,0)

有沒有辦法索引數據以找到大於 0 的元素的索引? 所以結果將是:

1 2 3 5 6

我嘗試使用 as.factor(data),但要獲得我想要的結果還需要幾個步驟。 謝謝。

我們可以在邏輯vector上使用which

which(data >0)
#[1] 1 2 3 5 6

另一種選擇是使用seq_along (但不像@akrunwhich方法那么簡單)

> seq_along(data)[data>0]
[1] 1 2 3 5 6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM