简体   繁体   中英

How to create a new variable based on another variable's value?

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:

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

Solution was indeed the simple suggestion from @akrun:

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

Hence, as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM