Jasmine Chong
Jasmine Chong

Reputation: 5

Function to create multiple lists in R

I'd like to be able to create a function that returns 3 lists. For instance:

Objects <- function(SetType, 2Type){
  1Set <- list()
  1Set$type <- SetType
  2Set <- list()
  2Set$type <- 2Type
  3Set <- list()

  return(1Set)
  return(2Set)
  return(3Set)
}

This only returns 1Set.

One option that I can think of is to create 2 functions that simply create 2Set and 3Set and then call upon them in the Objects function, but is there a better way of achieving this?

Upvotes: 0

Views: 1337

Answers (1)

BENY
BENY

Reputation: 323226

Also check this link

Objects <- function(SetType, 2Type){
  1Set <- list()
  1Set$type <- SetType
  2Set <- list()
  2Set$type <- 2Type
  3Set <- list()

  return(list(1Set,2Set,3Set))
}
Ret=Objects(SetType, 2Type)
1Set=Ret[[1]]
2Set=Ret[[2]]
3Set=Ret[[3]]

Upvotes: 1

Related Questions