aajit75
2012-Apr-10 06:44 UTC
[R] Assign value to new variable based on conditions on other variables
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.
aajit75
2012-Apr-10 07:16 UTC
[R] Assign value to new variable based on conditions on other variables
I have got solution using within function as below dd$Seg <- 1 dd <- within(dd, Seg[x2> 0 & x3> 200] <- 1) dd <- within(dd, Seg[x2> 100 & x3> 300] <- 2) dd <- within(dd, Seg[x2> 200 & x3> 400] <- 3) dd <- within(dd, Seg[x2> 300 & x3> 500] <- 4) I sthere any better way of doing it!! -- View this message in context: http://r.789695.n4.nabble.com/Assign-value-to-new-variable-based-on-conditions-on-other-variables-tp4544753p4544795.html Sent from the R help mailing list archive at Nabble.com.
Berend Hasselman
2012-Apr-10 08:21 UTC
[R] Assign value to new variable based on conditions on other variables
On 10-04-2012, at 08:44, aajit75 wrote:> 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] <-4Use & (vector wise logical operation) instead of && (only works on first element of vector) 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 Berend
David Winsemius
2012-Apr-10 12:59 UTC
[R] Assign value to new variable based on conditions on other variables
On Apr 10, 2012, at 3:16 AM, aajit75 wrote:> I have got solution using within function as below > > dd$Seg <- 1 > dd <- within(dd, Seg[x2> 0 & x3> 200] <- 1)In this instance the first of your assignments appears superfluous.> dd <- within(dd, Seg[x2> 100 & x3> 300] <- 2) > dd <- within(dd, Seg[x2> 200 & x3> 400] <- 3) > dd <- within(dd, Seg[x2> 300 & x3> 500] <- 4) > > I sthere any better way of doing it!! >dd <- with(dd, Seg <- min(findInterval(x2, c(-Inf ,100, 200, 300, Inf) ), findInterval(x3, C(-Inf ,300, 400, 500, Inf) ) ) ) David Winsemius, MD West Hartford, CT