I want to create a dummy variable, and I need to combine several conditions of other variables to assign value 0 to it. One of the variables used is newly created and I get the problem here.
attach(originaldata)
originaldata$hk_nonagr[hktype == 2 |hktype ==4 |hktype == 5] <- 1
originaldata$hk_nonagr[hktype == 1 |hktype == 3 |hktype == 6 |hktype == 7] <- 0
originaldata$hk_nonagr <- factor(originaldata$hk_nonagr,
levels = c(1, 0),
labels = c("yes", "no"))
This is my newly created variable. Then I want to use it to create another variable.
originaldata$hk_effort[urbanhk == 1|urbanhk == 2|urbanhk == 3|urbanhk == 4|urbanhk == 7] <- 1
originaldata$hk_effort[originaldata$hk_nonagr == 0 |yr_urbanhk == 9997|r_urbanhk == 5|r_urbanhk == 6|r_urbanhk ==8|r_urbanhk ==9] <- 0
Here I get the problem. Value 0 cannot be assigned. I tried
originaldata$hk_effort[originaldata$hk_nonagr == 0] <- 0
It doesn't work, that's why I think the problem is about the newly created variable. I get the same problem whenever I use a newly created variable in the condition.
I am a beginner in R, so please tell me whether this way to code is bad. In Stata, I am so used to write something like
replace x = 4 if (a == 1 | b ==3 ) & c != 8
But I now feel R users don't code in this way. Thank you in advance for any advice.