0

I have a vector containing string representing names of variables that should be in my final df. Those names could change every time based on other conditions.

x <- colnames(df)

y <- c("blue", "yellow", "red")

z <- setdiff(y,x)

Let's say my result now is that: z = c("blue", "red")

I would like a function that, if any element of vector y is missing from z, THEN the function will create a column on df with such element as variable name.


Here's my inconclusive attempt:

if (length(z) > 0) {
  for (i in z) {
   df$i <- NA
  }
}

The part I don't know how to do is pass i as an argument for creating a new variable on df. In my example: I should finally get df$yellow as a new variable of df.


I checked many posts, either I don't understand how it works, or they are not doing what I need, some for reference:

Cettt
  • 11,460
  • 7
  • 35
  • 58
Mirko Gagliardi
  • 69
  • 1
  • 10

2 Answers2

1

this is one possibility without any loops:

df <- data.frame(x = 1:5)
z <- c("blue", "red")

df[z] <- NA_character_

  x blue red
1 1   NA  NA
2 2   NA  NA
3 3   NA  NA
4 4   NA  NA
5 5   NA  NA
Cettt
  • 11,460
  • 7
  • 35
  • 58
0

Solution was indeed the simple suggestion from @akrun:

You can use [ instead of $ i.e. df[z] <- NA Reproducible mtcars[z] <- NA; head(mtcars)

Hence, as follows:

 if (length(z) > 0) {
  for (i in z) {
   df[i] <- NA
  }
}
Mirko Gagliardi
  • 69
  • 1
  • 10