Reputation: 197
Why is this following code not working. I want add a row to a tibble which I copied from the tibble before.
library(dplyr)
library(tibble)
tiris <- as_tibble(iris)
new_row <- tiris %>% tail(1)
tiris <- tiris %>% add_row(new_row)
Error: Column new_row
must be a 1d atomic vector or a list
Upvotes: 4
Views: 3935
Reputation: 39154
The new_row
you created is a one row tibble
, so I think what you need is the bind_rows
function from the dplyr package, which can combine two tibble
or data frame
by rows.
library(dplyr)
library(tibble)
tiris <- as_tibble(iris)
new_row <- tiris %>% tail(1)
# Combine tiris and new_row
tiris <- tiris %>% bind_rows(new_row)
See the last two rows of tiris
, which are identical. So I think bind_rows
works.
# View the results
tail(tiris, 2)
# # A tibble: 2 x 5
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# <dbl> <dbl> <dbl> <dbl> <fct>
# 1 5.90 3.00 5.10 1.80 virginica
# 2 5.90 3.00 5.10 1.80 virginica
Upvotes: 5