[R] another index question

jim holtman jholtman at gmail.com
Sat Dec 29 14:23:02 CET 2007


First of all, your last statement:

 > pre_ <- new[reoffend$pre07| reoffend$pre08 | reoffend$pre09 |
reoffend$pre10 |reoffend$pre11 | reoffend$pre12 | reoffend$pre13 |
reoffend$pre14 |reoffend$pre15 | reoffend$pre16<= 0 ] <- 'O'

says that if any of the variables except pre16 are non-zero, the
result will be "O".  It is not saying is any of the variables are less
than or equal to zero; only the last one.  You probably want:

pre_ [reoffend$pre07 <= 0| reoffend$pre08 <= 0 | reoffend$pre09 <= 0 |
reoffend$pre10 <= 0 |reoffend$pre11  <= 0| reoffend$pre12 <= 0 |
reoffend$pre13 <= 0 |
reoffend$pre14 <= 0 |reoffend$pre15 <= 0 | reoffend$pre16<= 0] <- "O"

You really don't need 'new'.

I think you also have the same problem in:

> pre_  <- new[reoffend$pre01111 | reoffend$pre012 |
reoffend$pre013>= 1] <-'V'

pre01111 and pre012 only have to be non-zero to make the statement
TRUE. you probably want:

pre_[reoffend$pre01111 >=1 | reoffend$pre012 >=1|
reoffend$pre013>= 1] <-'V'

On Dec 29, 2007 7:38 AM, Bob Green <bgreen at dyson.brisnet.org.au> wrote:
>
> I am hoping for some advice regarding another index problem. The code
> below is intended to assign a value of 'V' if values on certain
> variables are >= 1, assign a value of 'N' if values on certain
> variables are >= 1  on other variables and assign a 'O' if values on
> any variable which have a value <= 0.
>
> The outcome of this code is to assign a single 'O'. Clearly I have
> made a mistake somewhere.
>
> pre_  <- new <- rep(0, nrow(reoffend))
>  > pre_  <- new[reoffend$pre01111 | reoffend$pre012 |
> reoffend$pre013>= 1] <-'V'
>  > pre_  <- new[reoffend$pre02111>=1 | reoffend$pre02114>=1 |
> reoffend$pre2029>=1 | reoffend$pre0212>=1 | reoffend$pre0211>=1]<- 'V'
>  > pre_  <- new[reoffend$pre03a >=1 | reoffend$pre0311 >=1 |
> reoffend$pre0312 >=1| reoffend$pre03 >= 1] <- 'V'
>  > pre_  <- new[reoffend$pre04>=1 | reoffend$pre05 >=1 |
> reoffend$pre06 >=1] <- 'V'
>  > pre_  <- new[reoffend$pre07 >=1 | reoffend$pre08 >=1 |
> reoffend$pre09 >=1| reoffend$pre10 >= 1 |reoffend$pre11 >=1 |
> reoffend$pre12 >=1 | reoffend$pre13 >=1| reoffend$pre14 >= 1
> |reoffend$pre15 >=1| reoffend$pre16 >= 1 ] <- 'N'
>  > pre_  <- new[reoffend$pre01111 | reoffend$pre012 | reoffend$pre013<=0] <-'O'
>  > pre_  <- new[reoffend$pre02111<=0 | reoffend$pre02114<=0 |
> reoffend$pre2029<=0 | reoffend$pre0212<=0 | reoffend$pre0211 <=0] <-'O'
>  > pre_  <- new[reoffend$pre03a | reoffend$pre0311 | reoffend$pre0312
> | reoffend$pre03<=0] <- 'O'
>  > pre_  <- new[reoffend$pre04 | reoffend$pre05 | reoffend$pre06<=0] <- 'O'
>  > pre_ <- new[reoffend$pre07| reoffend$pre08 | reoffend$pre09 |
> reoffend$pre10 |reoffend$pre11 | reoffend$pre12 | reoffend$pre13 |
> reoffend$pre14 |reoffend$pre15 | reoffend$pre16<= 0 ] <- 'O'
>  >
>
> Secondly, once this is resolved I was hoping to change the prefix
> "pre_)" to "mhc_", repeat this code and join the pre_ and mhc_
> variables - so the new variable would have values such as 'VO" or
> 'NV' etc. I was hoping something like :  total <- c(pre_ , mhc_)
> would do this or am I misguided.
>
> Any assistance is much appreciated,
>
> Bob Green
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list