Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
r
communitycreator

What is the expand.grid() function in R?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The expand.grid() function in R is used to return a data frame from all combinations of the vector or factor objects supplied to it.

Syntax

expand.grid(...)
Syntax for the expand.grid() function

Parameter

This function takes the ... as a parameter value. It indicates the vectors, factors, or a list containing these.

Return value

The expand.grid() function returns a data frame that contains a row for each combination of the supplied vectors or factors.

Example

Let's look at the code below:

#creating factor objects
a <- c( 1, 2, 3)
b <- c( 4, 5, 6)
c <- c( 7, 8, 9)
# implementing the expand.grid() function
expand.grid(a, b, c)
Implementing the expand.grid() function

Explanation

  • Lines 2 to 4: We create factor objects, a, b and c.
  • Line 7: We call the expand.grid() function and pass the factor objects as its arguments. We obtain and print the result to the console.

RELATED TAGS

function
r
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring