Add_Column Not Adding Columns to Data Frame

Add_Column Not Adding Columns to Data Frame

I've used the add_column function from library(tibble) in my script, and it shows up fine in the console, but in the actual data frame df, it doesn't show that I've actually added any columns. The current structure of my data frame is a 60 x 17, but when I'm done adding my code shown below, it will end up being a 60 x 19, but when I use the following code, it doesn't give me any errors, and it still doesn't show the added columns.

add_column(df, 'Reading Depth'= extractdepth , .after = 1)
add_column(df, 'Half Depth'= halfdepth , .after = 2) 

Any idea on how to get both of my new column added to the data frame?

2

1 Answer

as Nathan points out above; you need to either update your object or create a new object.

Before you get to that you should however, always!, first load packages needed,

# install.packages(c("tidyverse"), dependencies = TRUE)
library(tidyverse)

Second, also always, creating some date, here very inspired by ?add_column,

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

Third, almost always, show the data,

df
#> # A tibble: 3 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3
#> 2     2     2
#> 3     3     1

great! Now to some solutions.

Solution one, an option where we crate a new object,

df_new <- df %>% add_column(z = 1:3, w = 0)
df_new
#> # A tibble: 3 x 4
#>       x     y     z     w
#>   <int> <int> <int> <dbl>
#> 1     1     3     1     0
#> 2     2     2     2     0
#> 3     3     1     3     0

Solution two, a solution where df is updated,

df <- df %>% add_column(z = -1:1, w = 0)
df
#> # A tibble: 3 x 4
#>       x     y     z     w
#>   <int> <int> <int> <dbl>
#> 1     1     3    -1     0
#> 2     2     2     0     0
#> 3     3     1     1     0

Note that <- is used to write into or crate a new object – as Nathan's comment pointed out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.