conditional coding question
On 08-11-2012, at 00:07, haps wrote:
I have a big dataset. I want to create a new factor variable with certain
conditions based on two existing numeric variables.
Existing variables: indinc (range: 0 to 16), groupinc (range -3 to 5)
Conditional values that 'incorp' will take:
If groupinc = 5, then ?cons?;
If groupinc is -3 : -2, AND indinc < 9, then ?ethnat?;
If groupinc is -2 : -1, AND indinc > 8, then ?civic?;
If groupinc is 1 : 4, AND indinc > 8, then ?libmul?;
The rest of the values should be coded as NA.
#here is my code after attaching the data (4408 is the number of
observations):#
incorp <-
for (i in 1:4408) {
if (groupinc[i] == 5) {
incorp[i] = 'cons'
} else if ((groupinc[i] == -3:-2) & (indinc[i] < 9)) {
incorp[i] = 'ethnat'
} else if ((groupinc[i] == 1:4) & (indinc[i] > 8)) {
incorp[i] = 'libmul'
} else if ((groupinc[i] == -2:-1) & (indinc[i] > 8)) {
incorp[i] = 'civic'
} else = NA
}
#error message#
Error: unexpected '=' in:
" incorp[i] = 'civic'
} else ="
So what's the question? The error message? It's a syntax error. else = NA is an invalid R construct. Change it to else NA Berend