Home > Net >  Function that returns named list in R
Function that returns named list in R

Time:09-21

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.

CodePudding user response:

Consider using dplyr::lst

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

and test as

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

$`"hello"`
[1] "hello"
  •  Tags:  
  • r
  • Related