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:

2
  • 2
    You can use [ instead of $ i.e. df[z] <- NA Reproducible mtcars[z] <- NA; head(mtcars) Commented Jul 17, 2019 at 13:28
  • 1
    Thank you @akrun this worked perfectly, I see I'm really missing the basics sometimes... Commented Jul 17, 2019 at 13:49

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.