add_row: Add rows to a data frame

View source: R/add.R

add_rowR Documentation

Add rows to a data frame

Description

This is a convenient way to add one or more rows of data to an existing data frame. See tribble() for an easy way to create an complete data frame row-by-row. Use tibble_row() to ensure that the new data has only one row.

add_case() is an alias of add_row().

Usage

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

Arguments

.data

Data frame to append to.

...

<dynamic-dots> Name-value pairs, passed on to tibble(). Values can be defined only for columns that already exist in .data and unset columns will get an NA value.

.before, .after

One-based row index where to add the new rows, default: after last row.

See Also

Other addition: add_column()

Examples

# add_row ---------------------------------
df <- tibble(x = 1:3, y = 3:1)

df %>% add_row(x = 4, y = 0)

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

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

# Use tibble_row() to add one row only
df %>% add_row(tibble_row(x = 4, y = 0))
try(df %>% add_row(tibble_row(x = 4:5, y = 0:-1)))

# Absent variables get missing values
df %>% add_row(x = 4)

# You can't create new variables
try(df %>% add_row(z = 10))

tibble documentation built on March 31, 2023, 11 p.m.