Guido Berning
Guido Berning

Reputation: 197

tibble add_row not tidy?

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

Answers (1)

www
www

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 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

Related Questions