I am looking to automate a process in R that was previously done by hand and is very time consuming. I'd like to add a series of observations from one dataframe to each unique variable in another. An example using data will probably illustrate this better...
Table one contains a number of observations for each animal, this is the table where I will want to add a set of rows for each type of animal.
| Animal | Colour | Temperament |
|---|---|---|
| Cat | Black | Calm |
| Dog | Beige | Anxious |
| Cat | White | Playful |
Table two shows the rows that should be applied to each animal.
| Colour | Temperament |
|---|---|
| Brown | Control |
| Beige | Control |
| White | Control |
The final table should look something like:
| Animal | Colour | Temperament |
|---|---|---|
| Cat | Black | Calm |
| Dog | Beige | Anxious |
| Cat | White | Playful |
| Cat | Brown | Control |
| Cat | Beige | Control |
| Cat | White | Control |
| Dog | Brown | Control |
| Dog | Beige | Control |
| Dog | White | Control |
Would someone be able to point me in the right direction? Pref using tidyverse over base R (but not essential :) )
CodePudding user response:
1.We create an easy to use and reproducible example data
d1 <- data.frame(an = c("c", "d", "c"),
cl = c("bl", "be", "wh"),
tm = c("cl", "an", "pl"))
d2 <- data.frame(cl = c("br", "be", "wh"),
tm = "cn")
2.Using expand_grid in combination with tidyr::full_join to expand the data.frame d1 to the desired form:
library(dplyr)
library(tidyr)
d1 %>%
full_join(expand_grid(d2, an = unique(d1$an)))
This returns:
an cl tm
1 c bl cl
2 d be an
3 c wh pl
4 c br cn
5 d br cn
6 c be cn
7 d be cn
8 c wh cn
9 d wh cn
