I have a beginner R user:
This is my dataset
factor1 <- c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,8,9, 9, 10, 10)
factor2 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,16,17, 18, 19, 20)
factor3 <- c("a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d")
factor4 <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150,160,170, 180, 190, NA)
dataset <- data.frame(factor1, factor2, factor3, factor4)
I created a new variable this way:
dataset$newvar <-"NA"
How to do the following:
I want newvar to take the value 1 if factor1>=5 and factor2<19 and (factor3="b" or factor3="c") and factor4 is different from missing and newvar is equal to missing
Ideally I want to specify different conditions, so some observations will be value 1, 2, 3 and 4 in the variable newvar dependent on the values of several other variables.
This is very simple and intuitive in STATA and would like to know if there is a simple and intuitive way to do the same in R.