iod
iod

Reputation: 7592

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?

Upvotes: 2

Views: 45

Answers (1)

akrun
akrun

Reputation: 887173

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))

Upvotes: 2

Related Questions