Examples for 'tibble::add_row'


Add rows to a data frame

Aliases: add_row add_case

Keywords:

### ** Examples

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

df %>% add_row(x = 4, y = 0)
# A tibble: 4 × 2
      x     y
  <dbl> <dbl>
1     1     3
2     2     2
3     3     1
4     4     0
# You can specify where to add the new rows
df %>% add_row(x = 4, y = 0, .before = 2)
# A tibble: 4 × 2
      x     y
  <dbl> <dbl>
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)
df %>% add_row(x = 4:5, y = 0:-1)
# A tibble: 5 × 2
      x     y
  <int> <int>
1     1     3
2     2     2
3     3     1
4     4     0
5     5    -1
# Use tibble_row() to add one row only
df %>% add_row(tibble_row(x = 4, y = 0))
# A tibble: 4 × 2
      x     y
  <dbl> <dbl>
1     1     3
2     2     2
3     3     1
4     4     0
try(df %>% add_row(tibble_row(x = 4:5, y = 0:-1)))
Error : All vectors must be size one, use `list()` to wrap.
✖ Column `x` is of size 2.
# Absent variables get missing values
df %>% add_row(x = 4)
# A tibble: 4 × 2
      x     y
  <dbl> <int>
1     1     3
2     2     2
3     3     1
4     4    NA
# You can't create new variables
try(df %>% add_row(z = 10))
Error : New rows can't add columns.
✖ Can't find column `z` in `.data`.

[Package tibble version 3.1.7 Index]