0

Is there a function in base R or a package which takes a list as argument and returns this list with the names set to the list items? Something along these lines:

named.list <- function(l) { names(l) <- l; l }

This is useful for the l?ply functions in plyr -- these functions keep the names of the argument list. Compare:

llply(c('a', 'b', 'c'), function(x) paste0('(', x, ')'))
 [[1]]
 [1] "(a)"

 [[2]]
 [1] "(b)"

 [[3]]
 [1] "(c)"
llply(named.list(c('a', 'b', 'c')), function(x) paste0('(', x, ')'))
 $a
 [1] "(a)"

 $b
 [1] "(b)"

 $c
 [1] "(c)"
krlmlr
  • 25,056
  • 14
  • 120
  • 217

1 Answers1

2

Do you want this for example?:

ll <- c('a', 'b', 'c')
ll <- setNames(ll,ll)
 ll
  a   b   c 
"a" "b" "c" 
agstudy
  • 119,832
  • 17
  • 199
  • 261