I want to use named vectors in a function of mine and I need the vector names from variables.
Example:
I want to create the vector
c(foo = 1, bar = -1)
in the following way:
a = "foo"
b = "bar"
c(a = 1, b = -1)
# where  c(a = 1, b = -1) == c(foo = 1, bar = -1)
Is there a way of using variables as names for vectors? Thanks in advance!
We can use setNames
setNames(c(1, -1), c(a, b))
#  foo bar 
#  1  -1 
Or another option is lst from dplyr to create a list and then unlist
library(dplyr)
unlist(lst(!! a := 1, !! b := -1))
#   foo bar 
#   1  -1 
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With