One option:
a <- "b"
> setNames(list(2),a)
$b
[1] 2
or the somewhat more "natural":
l <- list(2)
names(l) <- a
and if you look at the code in setNames
you'll see that these two methods are so identical that "the way to do this" in R really is basically to create the object, and then set the names, in two steps. setNames
is just a convenient way to do that.