How can I return multiple objects in an R function? In Java, I would make a Class, maybe Person which has some private variables and encapsulates, maybe, height, age, etc.
But in R, I need to pass around groups of data. For example, how can I make an R function return both an list of characters and an integer?
Unlike many other languages, R functions don’t return multiple objects in the strict sense. The most general way to handle this is to return a
listobject. So if you have an integerfooand a vector of stringsbarin your function, you could create a list that combines these items:Then
returnthis list.After calling your function, you can then access each of these with
newList$integerornewList$names.Other object types might work better for various purposes, but the
listobject is a good way to get started.