'R: Add/duplicate rows to a dataframe and replace column values when condition is met
I have two dataframes named TableA and TableB (reproducible example below).
Within TableA$D, whenever the value is NA, I would like to do two things:
1.) Add/duplicate the relevant rows "n" times, where "n" is defined by how many rows are in TableB.
2.) With the rows duplicated, replace the "NA"'s in TableA$D with the values from TableB.
Sometimes TableB will have 7 values, sometimes 100, and there is no prior knowledge of what values will appear in TableB.
A <- c("foo", "foo bar", "foo bar", "bar")
B <- c("bar", "foo", "bar bar", "foo")
C <- c("foo", "foo", "foo", "foo")
D <- c("X", NA, "X", NA)
TableA <- data.frame(A,B,C,D)
TableB <- data.frame(c(1,2,3,4,5,6,7))
I am trying to make like the result to look like this:
| A | B | C | D |
|---|---|---|---|
| foo | bar | foo | 1 |
| foo | bar | foo | 2 |
| foo | bar | foo | 3 |
| foo | bar | foo | 4 |
| foo | bar | foo | 5 |
| foo | bar | foo | 6 |
| foo | bar | foo | 7 |
| foo bar | foo | foo | X |
| foo bar | bar bar | foo | 1 |
| foo bar | bar bar | foo | 2 |
| foo bar | bar bar | foo | 3 |
| foo bar | bar bar | foo | 4 |
| foo bar | bar bar | foo | 5 |
| foo bar | bar bar | foo | 6 |
| foo bar | bar bar | foo | 7 |
| bar | foo | bar | X |
I have tried following this example Add values to dataframe when condition met in R however the logic used doesn't quite seem to fit my scenario.
Any help or tips would be greatly appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
