Skip to content

Instantly share code, notes, and snippets.

@agila5
Created April 12, 2019 10:03
Show Gist options
  • Save agila5/1880071f01228cf4ea3686677e1882e5 to your computer and use it in GitHub Desktop.
Save agila5/1880071f01228cf4ea3686677e1882e5 to your computer and use it in GitHub Desktop.
``` r
# packages
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(rlang)
# data
fake_data <- tibble(
A = 1
)
# loop
for(i in 1:10) {
var_name <- paste0("X", i)
fake_data <- fake_data %>%
mutate(!!var_name := 1)
}
# show result
fake_data
#> # A tibble: 1 x 11
#> A X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 1 1 1 1 1 1 1 1 1
```
<sup>Created on 2019-04-12 by the [reprex package](https://reprex.tidyverse.org) (v0.2.1)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment