This is a convenient way to add one or more columns to an existing data frame.
add_column( .data, ..., .before = NULL, .after = NULL, .name_repair = c("check_unique", "unique", "universal", "minimal") )
.data | Data frame to append to. |
---|---|
... | < |
.before, .after | One-based column index or column name where to add the new columns, default: after last column. |
.name_repair | Treatment of problematic column names:
This argument is passed on as |
Other addition:
add_row()
# add_column --------------------------------- df <- tibble(x = 1:3, y = 3:1) df %>% add_column(z = -1:1, w = 0) #> # A tibble: 3 × 4 #> x y z w #> <int> <int> <int> <dbl> #> 1 1 3 -1 0 #> 2 2 2 0 0 #> 3 3 1 1 0 df %>% add_column(z = -1:1, .before = "y") #> # A tibble: 3 × 3 #> x z y #> <int> <int> <int> #> 1 1 -1 3 #> 2 2 0 2 #> 3 3 1 1 # You can't overwrite existing columns try(df %>% add_column(x = 4:6)) #> Error : Column name `x` must not be duplicated. #> Use .name_repair to specify repair. # You can't create new observations try(df %>% add_column(z = 1:5)) #> Error : New columns must be compatible with `.data`. #> x New column has 5 rows. #> ℹ `.data` has 3 rows.