Function that returns named list in R

Viewed 171

I want to create a function that interprets the arguments one inputs in it as a vector of characters that afterwards its applied as names for the list. Something like this:

list_with_names<-function(...){
  b<-list(...)
  names(b)<-c(as.character(substitute(...)))
  return(b)
}

Also have tried this:

list_with_names<-function(...){
  b<-c(...)
  x1<-0
  for (a in b) {
    x1<-x1+1
    a[x1]<-c(as.character(substitute(...)))
  }
  names(b)<-c(a)
  return(b)
}

however it just reads the first argument.

1 Answers

Consider using dplyr::lst

list_with_names<-function(...){
  dplyr::lst(...)
   }

and test as

list_with_names("hello", "hello")
$`"hello"`
[1] "hello"

$`"hello"`
[1] "hello"
Related