im pretty new to R, i wonder how can do something like the following:
a = 'vol'
b = list(a=c(1,2,3))
actual output:
> b
$a
[1] 1 2 3
the output that I wish to have:
> b
$vol
[1] 1 2 3
CodePudding user response:
Mid-definition
a = 'vol'
b = setNames(list(c(1,2,3)), a)
b
# $vol
# [1] 1 2 3
The list(.)
can have names if you like, they will be overwritten. For instance, this produces the same result: setNames(list(a=c(1,2,3)), a)
(the initial name a=
is moot, and has nothing to do with the a
referenced as the second argument to setNames
).
Post-definition
a = 'vol'
b = list(a=c(1,2,3))
names(b) <- a
b
# $vol
# [1] 1 2 3
CodePudding user response:
Use lst
from dplyr
library(dplyr)
lst(!! a := c(1, 2, 3))
$vol
[1] 1 2 3