R: Get indices of all elements in a vector?

后端 未结 2 1538
离开以前
离开以前 2021-01-29 14:40

Simple questions, but I can\'t find an answer.

I have a vector

a<-c(5,6,7)

how can I get back the indices for all elements in my vec

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-29 15:31

    This is the purpose served by the seq_along function:

    seq_along(a)
    [1] 1 2 3
    > cbind( seq_along(a), a)
             a  
    [1,] "1" "a"
    [2,] "2" "b"
    [3,] "3" "c"
    >  data.frame( sq = seq_along(a), a)
      sq a
    1  1 a
    2  2 b
    3  3 c
    

提交回复
热议问题