Assign names to vector entries without assigning the vector a variable name?

前端 未结 2 1590
庸人自扰
庸人自扰 2020-12-06 04:16

In R, is it possible to assign names to components of a vector without first assigning that vector to a variable name? The normal way is obviously:

z <- 1         


        
相关标签:
2条回答
  • 2020-12-06 04:57

    Always thought this was a little cleaner, also don't need an additional package:

    z <- c(a=1, b=2, c=3)
    # z
    # a b c 
    # 1 2 3 
    
    0 讨论(0)
  • 2020-12-06 05:09

    How about using setNames(), which seems even cleaner/clearer than your suggested ideal?

    z <- setNames(1:3, c("a", "b", "c"))
    # z
    # a b c 
    # 1 2 3 
    
    0 讨论(0)
提交回复
热议问题