[R] help with if statement with two conditions

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Mon Dec 23 22:12:01 CET 2019


WITHOUT going through your code carefully (but where is v_lbs first
defined?), maybe something like this is what you want:

> x <- seq(-2,2,.25)
> x
 [1] -2.00 -1.75 -1.50 -1.25 -1.00 -0.75 -0.50 -0.25  0.00  0.25  0.50
 0.75  1.00
[14]  1.25  1.50  1.75  2.00
> x <- ifelse(x>0 & x<1, '<1', format(round(x,2), big.mark=",",
scientific=FALSE) )
> x
 [1] "-2.00" "-1.75" "-1.50" "-1.25" "-1.00" "-0.75" "-0.50" "-0.25" " 0.00"
[10] "<1"    "<1"    "<1"    " 1.00" " 1.25" " 1.50" " 1.75" " 2.00"

... or maybe not. You decide.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Dec 23, 2019 at 12:33 PM Steinmann, Kimberly using CDPR <
Kimberly.Steinmann using cdpr.ca.gov> wrote:

> Hi - i am not super familiar with R, but need to modify my predecessor's R
> code so that if a variable is >0 and < 0.5, it will be replaced with <1. I
> have looked at a bunch of forum threads on the subject, but cannot seem to
> get anything to work Any help at what i might be doing wrong much
> appreciated. I am definitely an R newbie!
>
> current code that works:
>       v_lbs_list <- ""
>       for (j in 2:(p_num_years+1)) {
>          if (is.na(v_trends_lbs[i, j])) {
>             v_lbs <- ' 0 '
>          } else if (v_trends_lbs[i, j] < 0.5) {
>             v_lbs <- ' $<$1 '
>          } else {
>             v_lbs <- format(round(v_trends_lbs[i, j]), big.mark=",",
> scientific=FALSE)
>          }
>          v_lbs_list <- paste(v_lbs_list, ' & ', v_lbs)
>       }
>
> my attempt to add a >0 that gets an error  pointing at the & sign:
>       v_lbs_list <- ""
>       for (j in 2:(p_num_years+1)) {
>          if (is.na(v_trends_lbs[i, j])) {
>             v_lbs <- ' 0 '
>          } else if (v_trends_lbs[i, j] < 0.5) & (v_trends_lbs[i, j] > 0) {
>             v_lbs <- ' $<$1 '
>          } else {
>             v_lbs <- format(round(v_trends_lbs[i, j]), big.mark=",",
> scientific=FALSE)
>          }
>          v_lbs_list <- paste(v_lbs_list, ' & ', v_lbs)
>       }
>
> Thanks for any help!
>
>
> CONFIDENTIALITY NOTICE: This e-mail message, including a...{{dropped:10}}
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list