Search code examples
rnamingassignradix

How to replace the name of an element during assignment of a named element?


If I have a named vector, and I'm trying to assign it into a new vector, but there I want it to have a different name, is there a way to avoid it coming out in the format newname.oldname, forcing me to rename()<- it manually?

Example:

a<-c(foo="baz")
a
#    foo 
#   "baz" 

b<-c(baz=a)
b
#  baz.foo 
#    "baz" 

I want to make it so b's name is only "baz", without the ".foo". Is there a way to achieve that directly?


Solution

  • We can do unname or as.vector

    c(baz = unname(a))
    # baz 
    #"baz" 
    

    Also with dplyr::lst, we can automatically name after unnameing

    dplyr::lst(!!unname(a))