问题
I have a number of variables with the same name but different suffixes. For example, var1, var2, var3, var4, var5, var6, ... and so on. Each of these variables has a random sequence of 0, 1, and 2. Using these variables, I am trying to create a new variable called testvariable
. If any of the already existing variables has 1, I will set testvariable
to 1. If they have 0 or 2, I will assign 0.
Is there a simple loop and/or ifelse statement I can use to create this variable? My real data is a lot more complex than this, so I don't want to copy and paste each individual variable and values.
Edit: This is for R.
回答1:
If I understand you correctly, if any of the variables like var1, var2, .., etc has a value 1, then the testvariable must be 1 else 0, then do:
Sample df:
var1 var2 var3 var4 var5
1 1 1 1 1 1
2 2 2 2 2 2
3 0 1 0 1 0
4 0 0 0 0 1
5 1 1 2 1 2
6 2 2 2 2 2
7 2 2 1 2 2
8 1 1 2 1 1
9 0 0 0 0 0
Code:
df$testvariable <- ifelse(rowSums(df[, grepl("var", names(df))] == 1) > 0, 1, 0)
Output:
var1 var2 var3 var4 var5 testvariable
1 1 1 1 1 1 1
2 2 2 2 2 2 0
3 0 1 0 1 0 1
4 0 0 0 0 1 1
5 1 1 2 1 2 1
6 2 2 2 2 2 0
7 2 2 1 2 2 1
8 1 1 2 1 1 1
9 0 0 0 0 0 0
回答2:
I would use mget
for this. mget
gives the variables as a list. Then you can check for each element of the list using sapply
and combine the results using any
. At the end I took advantage of your encoding of 0 and 1, but you can also use an if
statement.
var1 <- c(0,0,1,2)
var2 <- c(2,2,2,2)
var3 <- c(0,2,0,2)
var4 <- c(0,2,2,2)
any(sapply(mget(paste0("var", 1:4)), function(x) 1 %in% x)) * 1
#> [1] 1
any(sapply(mget(paste0("var", 2:4)), function(x) 1 %in% x)) * 1
#> [1] 0
来源:https://stackoverflow.com/questions/57190986/how-to-create-new-variables-using-loop-and-ifelse-statement