Hi Experts,

This may be simple question, I want to create new variable "seg" and assign
values to it based on some conditions satisfied by each observation.

Here is the example:
##Below are the conditions

##if variable x2 gt 0 and x3 gt 200 then seg should take value 1, 
##if variable x2 gt 100 and x3 gt 300 then seg should take value 2
##if variable x2 gt 200 and x3 gt 400 then seg should take value 3
##if variable x2 gt 300 and x3 gt 500 then seg should take value 4

id <- c(1,2,3,4,5)
x2 <- c(200,100,400,500,600)
x3 <- c(300,400,500,600,700)
dd <- data.frame(id,x2,x3)


dd$seg[dd$x2> 0 && dd$x3> 200] <-1
dd$seg[dd$x2> 100 && dd$x3> 300] <-2
dd$seg[dd$x2> 200 && dd$x3> 400] <-3
dd$seg[dd$x2> 300 && dd$x3> 500] <-4

I tried as above but it is not working for me. What is the correct and
efficient way to do this.

Thanks for the help in advance!!


--
View this message in context: 
http://r.789695.n4.nabble.com/Assign-value-to-new-variable-based-on-conditions-on-other-variables-tp4544753p4544753.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.

Reply via email to