Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mutate and create multiple dynamic variables all with value NA

I can create ONE variable with all values = NA in a data frame like this:

> library(rlang)
> library(dplyr)
> 
> myvar="x"
> mtcars%>% mutate(!!myvar:=NA) %>% head
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb  x
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 NA
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 NA
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 NA
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 NA
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 NA
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1 NA
>

My question is : how do I do this for multiple variables?

Here is an attempt:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> f <- function(...){
+     mylist =  enexprs(...)
+     lapply(mylist,function(x){x<- paste0(x,"=NA")})
+ }
> 
> f(x,y,z)
[[1]]
[1] "x=NA"

[[2]]
[1] "y=NA"

[[3]]
[1] "z=NA"

> 
> mtcars %>% mutate(!!!f(x,y,z)) %>% head
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb "x=NA"
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4   x=NA
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4   x=NA
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1   x=NA
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1   x=NA
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2   x=NA
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1   x=NA
                  "y=NA" "z=NA"
Mazda RX4           y=NA   z=NA
Mazda RX4 Wag       y=NA   z=NA
Datsun 710          y=NA   z=NA
Hornet 4 Drive      y=NA   z=NA
Hornet Sportabout   y=NA   z=NA
Valiant             y=NA   z=NA
> 

I am able to create 3 columns, but each has values like x=NA when it should be only NA.

Can someone point out my mistake?

>Solution :

You don’t need a function for this. We can just use a list and splice it into mutate with !!!:

library(dplyr)
newvars = list(x = NA,
               y = NA,
               z = NA)

mtcars %>% 
  mutate(!!! newvars) %>% 
  glimpse # for printing
#> Rows: 32
#> Columns: 14
#> $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,~
#> $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,~
#> $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16~
#> $ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180~
#> $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,~
#> $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.~
#> $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18~
#> $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,~
#> $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,~
#> $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,~
#> $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,~
#> $ x    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
#> $ y    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
#> $ z    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~

Created on 2022-01-13 by the reprex package (v2.0.1)

We could use the same logic in a function:

library(dplyr)

f <- function(...) {
  dots <- rlang::enexprs(...)
  expr_ls <- purrr::set_names(dots) %>% purrr::map(~ NA)
  tibble(!!! expr_ls)
}

mtcars %>% 
  mutate(f(x, y, z)) %>% 
  glimpse # for printing

#> Rows: 32
#> Columns: 14
#> $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,~
#> $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,~
#> $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16~
#> $ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180~
#> $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,~
#> $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.~
#> $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18~
#> $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,~
#> $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,~
#> $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,~
#> $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,~
#> $ x    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
#> $ y    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
#> $ z    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~

Created on 2022-01-13 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading