R: Get indices of all elements in a vector?

后端 未结 2 1545
离开以前
离开以前 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:05

    If you like something elementary, you can try:

    a<-c(5,6,7)
    b <- 1:length(a)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题