Is there a way to create a data frame with rows for combinations of variables in other data frames?How to join (merge) data frames (inner, outer, left, right)Drop data frame columns by nameCreate an empty data.frameMerge data frames of unequal number of rowsCreate vector of data frame subsets based on group by of columnsChanging Column Names in a List of Data Frames in RHow to substitute some values of one dataframe with other data frame in R?How to keep merging data frames using a function in R?Python Pandas - Find difference between two data framesPandas create a data frame based on two other 'sub' frames
What is the name of this OOB notification method/popup, and is it customizable?
Are there any features that help with the roll to avoid the destruction of a Wand of Fireballs when using the last charge?
Does anyone know what these symbols mean?
Averting Real Women Don’t Wear Dresses
Most elegant way to write a one shot IF
Why do I need two parameters in an HTTP parameter pollution attack?
What is "oversubscription" in Networking?
What could a reptilian race tell by candling their eggs?
One folder two different locations on ubuntu 18.04
Needle Hotend for nonplanar printing
Could human civilization live 150 years in a nuclear-powered aircraft carrier colony without resorting to mass killing/ cannibalism?
Is there reliable evidence that depleted uranium from the 1999 NATO bombing is causing cancer in Serbia?
Mean Value Theorem: Continuous or Defined?
Details of video memory access arbitration in Space Invaders
The Confused Alien
Who are these Discworld wizards from this picture?
How exactly is a normal force exerted, at the molecular level?
How do I tell the reader that my character is autistic in Fantasy?
Does Anosov geodesic flow imply asphericity?
Why did this meteor appear cyan?
Most importants new papers in computational complexity
Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?
How to fix a dry solder pin in BGA package?
Why was Mal so quick to drop Bester in favour of Kaylee?
Is there a way to create a data frame with rows for combinations of variables in other data frames?
How to join (merge) data frames (inner, outer, left, right)Drop data frame columns by nameCreate an empty data.frameMerge data frames of unequal number of rowsCreate vector of data frame subsets based on group by of columnsChanging Column Names in a List of Data Frames in RHow to substitute some values of one dataframe with other data frame in R?How to keep merging data frames using a function in R?Python Pandas - Find difference between two data framesPandas create a data frame based on two other 'sub' frames
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have several data frames containing one column each. I want to create a data frame which has rows for each possible combination of the values in the other data frames.
I tried a workaround with nested for loops for each data frame but since i have about 20 data frames it became tedious quite fast.
# the data frames I already have
df1 <- data.frame(c("A","B","C"))
colnames(df1) <- c("x")
df2 <- data.frame(c("ALPHA","BRAVO","CHARLIE"))
colnames(df2) <- c("y")
# the data frame I want to create
df3 <- data.frame(c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
c("ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE"))
colnames(df3) <- c("x", "y")
r dataframe
add a comment |
I have several data frames containing one column each. I want to create a data frame which has rows for each possible combination of the values in the other data frames.
I tried a workaround with nested for loops for each data frame but since i have about 20 data frames it became tedious quite fast.
# the data frames I already have
df1 <- data.frame(c("A","B","C"))
colnames(df1) <- c("x")
df2 <- data.frame(c("ALPHA","BRAVO","CHARLIE"))
colnames(df2) <- c("y")
# the data frame I want to create
df3 <- data.frame(c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
c("ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE"))
colnames(df3) <- c("x", "y")
r dataframe
For completeness:data.table::CJ
– r2evans
Mar 25 at 12:48
add a comment |
I have several data frames containing one column each. I want to create a data frame which has rows for each possible combination of the values in the other data frames.
I tried a workaround with nested for loops for each data frame but since i have about 20 data frames it became tedious quite fast.
# the data frames I already have
df1 <- data.frame(c("A","B","C"))
colnames(df1) <- c("x")
df2 <- data.frame(c("ALPHA","BRAVO","CHARLIE"))
colnames(df2) <- c("y")
# the data frame I want to create
df3 <- data.frame(c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
c("ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE"))
colnames(df3) <- c("x", "y")
r dataframe
I have several data frames containing one column each. I want to create a data frame which has rows for each possible combination of the values in the other data frames.
I tried a workaround with nested for loops for each data frame but since i have about 20 data frames it became tedious quite fast.
# the data frames I already have
df1 <- data.frame(c("A","B","C"))
colnames(df1) <- c("x")
df2 <- data.frame(c("ALPHA","BRAVO","CHARLIE"))
colnames(df2) <- c("y")
# the data frame I want to create
df3 <- data.frame(c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
c("ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE"))
colnames(df3) <- c("x", "y")
r dataframe
r dataframe
asked Mar 25 at 12:35
erikfjonssonerikfjonsson
487 bronze badges
487 bronze badges
For completeness:data.table::CJ
– r2evans
Mar 25 at 12:48
add a comment |
For completeness:data.table::CJ
– r2evans
Mar 25 at 12:48
For completeness:
data.table::CJ
– r2evans
Mar 25 at 12:48
For completeness:
data.table::CJ
– r2evans
Mar 25 at 12:48
add a comment |
2 Answers
2
active
oldest
votes
One option is to use tidyr::crossing
tidyr::crossing(df1, df2)
# df1$x df2$y
# <fct> <fct>
#1 A ALPHA
#2 A BRAVO
#3 A CHARLIE
#4 B ALPHA
#5 B BRAVO
#6 B CHARLIE
#7 C ALPHA
#8 C BRAVO
#9 C CHARLIE
You can just add all the dataframes in it and it would give you output for all combinations of it.
In base R, you could also use merge
in Reduce
by putting all the dataframes in one list.
Reduce(merge, list(df1, df2))
add a comment |
We can use CJ
from data.table
library(data.table)
CJ(x= df1$x, y = df2$y)
# x y
#1: A ALPHA
#2: A BRAVO
#3: A CHARLIE
#4: B ALPHA
#5: B BRAVO
#6: B CHARLIE
#7: C ALPHA
#8: C BRAVO
#9: C CHARLIE
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55337933%2fis-there-a-way-to-create-a-data-frame-with-rows-for-combinations-of-variables-in%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
One option is to use tidyr::crossing
tidyr::crossing(df1, df2)
# df1$x df2$y
# <fct> <fct>
#1 A ALPHA
#2 A BRAVO
#3 A CHARLIE
#4 B ALPHA
#5 B BRAVO
#6 B CHARLIE
#7 C ALPHA
#8 C BRAVO
#9 C CHARLIE
You can just add all the dataframes in it and it would give you output for all combinations of it.
In base R, you could also use merge
in Reduce
by putting all the dataframes in one list.
Reduce(merge, list(df1, df2))
add a comment |
One option is to use tidyr::crossing
tidyr::crossing(df1, df2)
# df1$x df2$y
# <fct> <fct>
#1 A ALPHA
#2 A BRAVO
#3 A CHARLIE
#4 B ALPHA
#5 B BRAVO
#6 B CHARLIE
#7 C ALPHA
#8 C BRAVO
#9 C CHARLIE
You can just add all the dataframes in it and it would give you output for all combinations of it.
In base R, you could also use merge
in Reduce
by putting all the dataframes in one list.
Reduce(merge, list(df1, df2))
add a comment |
One option is to use tidyr::crossing
tidyr::crossing(df1, df2)
# df1$x df2$y
# <fct> <fct>
#1 A ALPHA
#2 A BRAVO
#3 A CHARLIE
#4 B ALPHA
#5 B BRAVO
#6 B CHARLIE
#7 C ALPHA
#8 C BRAVO
#9 C CHARLIE
You can just add all the dataframes in it and it would give you output for all combinations of it.
In base R, you could also use merge
in Reduce
by putting all the dataframes in one list.
Reduce(merge, list(df1, df2))
One option is to use tidyr::crossing
tidyr::crossing(df1, df2)
# df1$x df2$y
# <fct> <fct>
#1 A ALPHA
#2 A BRAVO
#3 A CHARLIE
#4 B ALPHA
#5 B BRAVO
#6 B CHARLIE
#7 C ALPHA
#8 C BRAVO
#9 C CHARLIE
You can just add all the dataframes in it and it would give you output for all combinations of it.
In base R, you could also use merge
in Reduce
by putting all the dataframes in one list.
Reduce(merge, list(df1, df2))
edited Mar 25 at 12:48
answered Mar 25 at 12:42
Ronak ShahRonak Shah
62.7k10 gold badges46 silver badges80 bronze badges
62.7k10 gold badges46 silver badges80 bronze badges
add a comment |
add a comment |
We can use CJ
from data.table
library(data.table)
CJ(x= df1$x, y = df2$y)
# x y
#1: A ALPHA
#2: A BRAVO
#3: A CHARLIE
#4: B ALPHA
#5: B BRAVO
#6: B CHARLIE
#7: C ALPHA
#8: C BRAVO
#9: C CHARLIE
add a comment |
We can use CJ
from data.table
library(data.table)
CJ(x= df1$x, y = df2$y)
# x y
#1: A ALPHA
#2: A BRAVO
#3: A CHARLIE
#4: B ALPHA
#5: B BRAVO
#6: B CHARLIE
#7: C ALPHA
#8: C BRAVO
#9: C CHARLIE
add a comment |
We can use CJ
from data.table
library(data.table)
CJ(x= df1$x, y = df2$y)
# x y
#1: A ALPHA
#2: A BRAVO
#3: A CHARLIE
#4: B ALPHA
#5: B BRAVO
#6: B CHARLIE
#7: C ALPHA
#8: C BRAVO
#9: C CHARLIE
We can use CJ
from data.table
library(data.table)
CJ(x= df1$x, y = df2$y)
# x y
#1: A ALPHA
#2: A BRAVO
#3: A CHARLIE
#4: B ALPHA
#5: B BRAVO
#6: B CHARLIE
#7: C ALPHA
#8: C BRAVO
#9: C CHARLIE
answered Mar 25 at 15:08
akrunakrun
443k14 gold badges245 silver badges325 bronze badges
443k14 gold badges245 silver badges325 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55337933%2fis-there-a-way-to-create-a-data-frame-with-rows-for-combinations-of-variables-in%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
For completeness:
data.table::CJ
– r2evans
Mar 25 at 12:48