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

1 回答 1

2

例如,你想要这个吗?:

ll <- c('a', 'b', 'c')
ll <- setNames(ll,ll)
 ll
  a   b   c 
"a" "b" "c" 
于 2013-03-14T21:01:02.790 回答