# A tibble: 3 × 3 Name Age Enrolled <chr> <dbl> <lgl> 1 Alice 20 TRUE 2 Bob 22 FALSE 3 Charlie 21 TRUE
# add two more students' information as two extra rowsstudents %>%add_row(Name =c("Tom", "Mary"), Age =c(28, 18), Enrolled =c(F, T))
Output:
# A tibble: 5 × 3 Name Age Enrolled <chr> <dbl> <lgl> 1 Alice 20 TRUE 2 Bob 22 FALSE 3 Charlie 21 TRUE 4 Tom 28 FALSE 5 Mary 18 TRUE
Absent variables will create NA values.
students %>%add_row(# the missing column "Enrolled" is filled with 'NA' in the outputName =c("Tom", "Mary"), Age =c(28, 18))
Output:
# A tibble: 5 × 3 Name Age Enrolled <chr> <dbl> <lgl> 1 Alice 20 TRUE 2 Bob 22 FALSE 3 Charlie 21 TRUE 4 Tom 28 NA 5 Mary 18 NA
Use .before (or .after) to specify the first tibble’s row index where the second tibble should be inserted. (These arguments are also available in add_column() and mutate() to specify new columns’ position.)
students %>%add_row(Name =c("Tom", "Mary"), Age =c(28, 18), # insert the 2nd tibble before the 1st tibble's 3rd row.before =3)
Output:
# A tibble: 5 × 3 Name Age Enrolled <chr> <dbl> <lgl> 1 Alice 20 TRUE 2 Bob 22 FALSE 3 Tom 28 NA 4 Mary 18 NA 5 Charlie 21 TRUE
You can directly combine two tibbles by rows.
# create a second tibble to combine by rowsmore_students <-tibble(Name =c("Tom", "Jack", "Clement"), Age =c(28, 22, 34), Enrolled =c(F, F, T)) # combine the two tibblesstudents %>%add_row(more_students, .before =3)
Output:
# A tibble: 6 × 3 Name Age Enrolled <chr> <dbl> <lgl> 1 Alice 20 TRUE 2 Bob 22 FALSE 3 Tom 28 FALSE 4 Jack 22 FALSE 5 Clement 34 TRUE 6 Charlie 21 TRUE