The dplyr
function add_case()
allows for adding cases to an existing data set, whether at the end of the set or a predefined place of the table. Note that there exist a function called add_row()
which does exactly the same as add_case()
.
The syntax is simple. Between the parentheses of add_case()
, each of the variables found in the original table or data frame must be given a value using the format variable = "value"
. If one (or several) of the variables is missing or not given a value, NA
will become the value for that (these) variable(s). Here are a couple of illustrations using the data frame Orange.
In the example below, the code gives a value to each of the variables:
Orange %>% add_case(Tree = "66", age = "666", circumference = "6666")
The resulting table has a new, 36th row that contain the given values at the right places.
In this second example, let’s see what happens when one of the variable is omitted in the code:
Orange %>% add_case(Tree = "66", circumference = "6666")
This code omitted a value for the variable age
which has been automatically replaced by NA
.
In the examples above, there is no mention or indication of a position for the new case. It is however possible to place that new case anywhere in the resulting table. By using .after =
or .before =
, you may decide of the position:
Orange %>% add_case(Tree = "66", circumference = "6666", .before = 8)
By indicating .before = 8
, the new data take the place of the 8th row of the original data set.