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?
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.
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).
To find the unique pair combinations of an R data frame column values, we can use combn function along with unique function.
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.
We can use expand.grid
to get all the possible combinations between the two vectors.
expand.grid(x,y)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With