1

I have this example script:

library(tidyverse)

placeholder <- c("foo", "bar", "baz", "bash")

x <- 1
for (name in placeholder) {
    iris <- iris %>% add_column(name = rep("example", nrow(iris)),
                                .after = x)
    x <- x + 2

}

What I would like to do, is to insert a column after each of the flower characteristics, with columns named from the elements in placeholder.

Expected output:

  Sepal.Length     foo Sepal.Width     bar Petal.Length     baz Petal.Width    bash Species
1          5.1 example         3.5 example          1.4 example         0.2 example  setosa
2          4.9 example         3.0 example          1.4 example         0.2 example  setosa
3          4.7 example         3.2 example          1.3 example         0.2 example  setosa
4          4.6 example         3.1 example          1.5 example         0.2 example  setosa
5          5.0 example         3.6 example          1.4 example         0.2 example  setosa
6          5.4 example         3.9 example          1.7 example         0.4 example  setosa

However my for loop labels the first column in the for loop name, then terminates with an error saying Error: Column name already exists.

What I get:

  Sepal.Length    name Sepal.Width Petal.Length Petal.Width Species
1          5.1 example         3.5          1.4         0.2  setosa
2          4.9 example         3.0          1.4         0.2  setosa
3          4.7 example         3.2          1.3         0.2  setosa
4          4.6 example         3.1          1.5         0.2  setosa
5          5.0 example         3.6          1.4         0.2  setosa
6          5.4 example         3.9          1.7         0.4  setosa
Nautica
  • 2,004
  • 1
  • 12
  • 35

1 Answers1

2

We can use the assignment operator (:=) with name evaluated (!!) on the lhs for setting the values of placeholder as the column names

x <- 1
for (name in placeholder) {
    iris <- iris %>% add_column(!! name := rep("example", nrow(iris)),
                            .after = x)
     x <- x + 2

 }
head(iris)
#  Sepal.Length     foo Sepal.Width     bar Petal.Length     baz Petal.Width    bash Species
#1          5.1 example         3.5 example          1.4 example         0.2 example  setosa
#2          4.9 example         3.0 example          1.4 example         0.2 example  setosa
#3          4.7 example         3.2 example          1.3 example         0.2 example  setosa
#4          4.6 example         3.1 example          1.5 example         0.2 example  setosa
#5          5.0 example         3.6 example          1.4 example         0.2 example  setosa
#6          5.4 example         3.9 example          1.7 example         0.4 example  setosa
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Could you expand more on why we need `:=` as the assignment operator when used in conjunction with `!!`? Or do you know any good articles discussing their usage? – Nautica Jan 22 '19 at 10:58
  • 1
    @VY. Sorry for the delay in response. It is not the same assignment operator from `data.table`, but it is the one introduced in tidyverse when we want to assign names on the lhs based on an identifier object. The `!!` is used to evaluate the value in the object instead of assigning it as `name`. In some other languages, this operator is called as declarative assignment i.e. declare a variable with identifier and assign some value. Here, it is slightly different – akrun Jan 22 '19 at 11:17
  • @VY You can also check [here](https://www.tidyverse.org/). I think the vignettes are also included there – akrun Jan 22 '19 at 11:22