0

I would like to add columns in a dataframe (filled in with NA) from an existing vector.
Here's my example (I want 2 new columns "State" and "Type" with NA)
Many thanks in advance !

names_variables <- c("Sepal.Length",
                     "Sepal.Width",
                     "Petal.Length",
                     "Petal.Width",
                     "Species",
                     "State",
                     "Type")

difference <- setdiff(names_variables,names(iris))

# Not working
if (length(difference>0)) {
  resultat <- iris %>% 
    mutate(
      for (var_to_add in (difference)) {
        !!sym(var_to_add) := NA)
      }
    )
}
Damien Dotta
  • 771
  • 3
  • 13

1 Answers1

1

I'm not exactly sure what the result should be, but we can create a named vector of new variables and splice it into mutate with !!!:

library(dplyr)

names_variables <- c("Sepal.Length",
                     "Sepal.Width",
                     "Petal.Length",
                     "Petal.Width",
                     "Species",
                     "State",
                     "Type")

difference <- setdiff(names_variables,names(iris))
new_vars <- setNames(rep(NA, length(difference)), difference)

iris %>%
  as_tibble() %>% # for printing
  mutate(!!! new_vars)

#> # A tibble: 150 x 7
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species State Type 
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>   <lgl> <lgl>
#>  1          5.1         3.5          1.4         0.2 setosa  NA    NA   
#>  2          4.9         3            1.4         0.2 setosa  NA    NA   
#>  3          4.7         3.2          1.3         0.2 setosa  NA    NA   
#>  4          4.6         3.1          1.5         0.2 setosa  NA    NA   
#>  5          5           3.6          1.4         0.2 setosa  NA    NA   
#>  6          5.4         3.9          1.7         0.4 setosa  NA    NA   
#>  7          4.6         3.4          1.4         0.3 setosa  NA    NA   
#>  8          5           3.4          1.5         0.2 setosa  NA    NA   
#>  9          4.4         2.9          1.4         0.2 setosa  NA    NA   
#> 10          4.9         3.1          1.5         0.1 setosa  NA    NA   
#> # … with 140 more rows

Created on 2021-06-09 by the reprex package (v0.3.0)


Alternatively with base r:

names_variables <- c("Sepal.Length",
                     "Sepal.Width",
                     "Petal.Length",
                     "Petal.Width",
                     "Species",
                     "State",
                     "Type")

difference <- setdiff(names_variables, names(iris))

iris[, difference] <- NA

head(iris)

#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species State Type
#> 1          5.1         3.5          1.4         0.2  setosa    NA   NA
#> 2          4.9         3.0          1.4         0.2  setosa    NA   NA
#> 3          4.7         3.2          1.3         0.2  setosa    NA   NA
#> 4          4.6         3.1          1.5         0.2  setosa    NA   NA
#> 5          5.0         3.6          1.4         0.2  setosa    NA   NA
#> 6          5.4         3.9          1.7         0.4  setosa    NA   NA

Created on 2021-06-09 by the reprex package (v0.3.0)

TimTeaFan
  • 17,549
  • 4
  • 18
  • 39