Skip to contents

Insert one or more rows into an existing data frame.

Usage

add_row(data, x, before = NULL, after = NULL)

Arguments

data

A data frame.

x

A named list of values to insert. Unused column names will be NA.

before, after

An integer vector for the column index. Defaults to after last row.

Value

data.frame

References

Kirill Muller, Hadley Wickham, and Romain francois (Rstudio) (2017). tibble: Simple Data Frames. R package version 1.4.2. https://CRAN.R-project.org/package=tibble. Git commit 294c3cffad38f8f9f277c848742a90984fe239e3

Examples

#----------------------------------------------------------------------------
# add_row() examples
#----------------------------------------------------------------------------
library(bkdat)

df <- data.frame(x = 1:3, y = 3:1)

add_row(df, list(x = 4, y = 0))
#>   x y
#> 1 1 3
#> 2 2 2
#> 3 3 1
#> 4 4 0

# You can specify where to add the new rows
add_row(df, list(x = 4, y = 0), before = 2)
#>   x y
#> 1 1 3
#> 2 4 0
#> 3 2 2
#> 4 3 1

# You can supply vectors, to add multiple rows (this isn't
# recommended because it's a bit hard to read)
add_row(df, list(x = 4:5, y = 0:-1))
#>   x  y
#> 1 1  3
#> 2 2  2
#> 3 3  1
#> 4 4  0
#> 5 5 -1

# Absent variables get missing values
add_row(df, list(x = 4))
#>   x  y
#> 1 1  3
#> 2 2  2
#> 3 3  1
#> 4 4 NA

# You can't create new variables
if (FALSE) { # \dontrun{
add_row(df, list(z = 10))
} # }