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.
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
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 at 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.
On Nov 7, 2012, at 3:07 PM, 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 <-You are making an inappropriate double assignment. This assignment will shoot off your foot. I believe that for loops return NULL, so even if your loop succeeded in making all the assignments inside the body which it did not and I believe could not even with syntactic surgery for the reason expanded on below, the incorp value would be NULL afterward.> incorp= 1:10; incorp <- for (i in seq_along(incorp) ) { incorp[i] = 2*incorp[i] } > incorpNULL> for (i in 1:4408) { > if (groupinc[i] == 5) { > incorp[i] = 'cons' > } else if ((groupinc[i] == -3:-2) & (indinc[i] < 9)) {I doubt very much that you should be testing for (groupinc[i] == -3:-2) I suspect you what to be testing whether groupinc[i] is in the range -3 to -2 For that you have a couple of choices. If you know for a fact that groupinc[i] will be integer valued (and is not really a double/float value then you could do: if ( groupinc[i[ %in% -3:-2 ) If group inc[i] is a floating point number you should test: if( groupinc[i] >= -3 & groupinc[i] <= -2)> 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 ="That is a syntactic error, but I suspect you have semantic errors as mentioned above.> > > > -- > 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 at 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.David Winsemius, MD Alameda, CA, USA
Thank you all very much for your helpful comments and showing where I went wrong. After so many trials, the code below seems to work: incorp <- with(data7.13.11, ifelse(groupinc %in% c(5), 'cons', ifelse(groupinc %in% c(-3,-2) & indinc < 9, 'ethnat', ifelse(groupinc %in% c(1,2,3,4) & indinc > 8, 'libmul', ifelse(groupinc %in% c(-2,-1) & indinc > 8, 'civic', NA))))) Let me know if it's just an illusion on my part -- View this message in context: http://r.789695.n4.nabble.com/conditional-coding-question-tp4648801p4648915.html Sent from the R help mailing list archive at Nabble.com.