0

I tried reading through R's documentation on the add_column function, but I'm a little confused as to the examples it provides. See below:

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

df %>% add_column(z = -1:1, w = 0)
df %>% add_column(z = -1:1, .before = "y")

# You can't overwrite existing columns
try(df %>% add_column(x = 4:6))

# You can't create new observations
try(df %>% add_column(z = 1:5))

What is the purpose of these letters that are being assigned a range? Eg:

z = 1:5

My understanding from the documentation is that add_column() takes in a dataframe and appends it in position based on the .before and .after arguments defaulting to the end of the dataframe.

I'm a little confused here. There is also a "..." argument that takes in Name-value pairs. Is that what I'm seeing with "z = 1:5"? What is the functional purpose of this?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Stephen
  • 133
  • 1
  • 9
  • `add_column` appends one or more columns to an existing data.frame. That what the `...` means, i.e. you could add one or two or ... columns. With `.before` or `.after` you could specify where to insert the new column(s). Finally you specify the new columns as name-value pairs, i.e. with `z=-1:1` to specify that you want to a new column with name `z` and values `-1:1`. The ranges are just short for `c(-1,0,1)`. – stefan Feb 05 '21 at 18:41

1 Answers1

1

data.frame columns always have a name in R, no exception.

Since add_column adds new columns, you need to specify names for these columns.

… well, technically you don’t need to. The following works:

df %>% add_column(1 : 3)

But add_column auto-generates the column name based on the expression you pass it, and you might not like the result (in this case, it’s literally 1:3, which isn’t a convenient name to work with).

Conversely, the following also works and is perfectly sensible:

z = 1 : 3
df %>% add_column(z)

Result:

# A tibble: 3 x 3
      x     y     z
  <int> <int> <int>
1     1     3     1
2     2     2     2
3     3     1     3
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214