Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create data frame with all possible combinations of vectors x and y? [duplicate]

Tags:

r

I have 2 vectors, x and y:

x <- seq(from=21.6, to=22, by=0.01)
y <- seq(from=58.77, to=58.93, by=0.01)

I want to create a data frame df1<-data.frame(x,y) with all the possible combinations of x and y. How can I do this?

like image 821
Ignacio Avatar asked Aug 28 '15 19:08

Ignacio


People also ask

How do you generate all possible combinations of items from one list in R?

grid() method. expand. grid() in R can be used to generate a data frame where the rows are all possible unique combinations formed on taking elements from the argument vectors.

How do you make all combinations of a vector in R?

To create combination of multiple vectors, we can use expand. grid function. For example, if we have six vectors say x, y, z, a, b, and c then the combination of vectors can be created by using the command expand. grid(x,y,z,a,b,c).

How do I get unique combinations in R?

To find the unique pair combinations of an R data frame column values, we can use combn function along with unique function.

What is crossing function in R?

crossing() is a wrapper around expand_grid() that de-duplicates and sorts its inputs; nesting() is a helper that only finds combinations already present in the data.


1 Answers

We can use expand.grid to get all the possible combinations between the two vectors.

expand.grid(x,y)
like image 61
akrun Avatar answered Sep 24 '22 06:09

akrun