dplyr row added with add_row disappears

Hi,

i am new to R as i'm trying to analyse a very large dataset excel can't handle any longer;)
I have a simple question regarding the add_row function from the dplyr package.
I have created a tibble "test" with the columns x,y and z.
If I add a row is seems to work, but when i look at the head of the tibble the added row does not appear. I couldn't find any explanation for this behaviour, it would be great if someone has an idea what i do wrong...

> test
># A tibble: 0 x 3
># … with 3 variables: x <dbl>, y <int>, z <int>
> add_row(test, x = 0, y = 1, z = 2)
># A tibble: 1 x 3
>      x     y     z
>  <dbl> <dbl> <dbl>
>1     0     1     2
> head(test)
># A tibble: 0 x 3
># … with 3 variables: x <dbl>, y <int>, z <int>

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. This one, however, doesn't quite need it.

The add_row line dutifully returns a value, but it is ephemeral because it wasn't assigned to a name that would preserve it.

Try

add_row(test, x = 0, y = 1, z = 2) -> test

dplyr doesn't perform in-place modifications, you have to explicitly assign the changes to a variable

library(dplyr)

test <- data.frame(x = integer(),
                   y = integer(),
                   z = integer())

test <- add_row(test, x = 0, y = 1, z = 2)
test
#>   x y z
#> 1 0 1 2

Created on 2020-03-31 by the reprex package (v0.3.0.9001)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.