Hello,

Sorry, there's a mistake repeated in 3 code lines. Corrected:

incorp <- rep(NA, 4408)
incorp[groupinc == 5] <- 'cons'
incorp[(groupinc %in% -3:-2) & (indinc < 9)] <- 'ethnat'
incorp[(groupinc %in% 1:4) & (indinc > 8)] <- 'libmul'
incorp[(groupinc %in% -2:-1) & (indinc > 8)] <- 'civic'


Hope this helps,

Rui Barradas
Em 08-11-2012 11:13, Rui Barradas escreveu:
Hello,

Try the following.


incorp <- rep(NA, 4408)
incorp[groupinc == 5] <- 'cons'
incorp[(groupinc == -3:-2) & (indinc < 9)] <- 'ethnat'
incorp[(groupinc == 1:4) & (indinc > 8)] <- 'libmul'
incorp[(groupinc == -2:-1) & (indinc > 8)] <- 'civic'


Hope this helps,

Rui Barradas
Em 07-11-2012 23:07, haps escreveu:
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 ="



--
View this message in context: http://r.789695.n4.nabble.com/conditional-coding-question-tp4648801.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to