[R] help with if statement with two conditions

Steinmann, Kimberly@CDPR K|mber|y@Ste|nm@nn @end|ng |rom cdpr@c@@gov
Mon Dec 23 20:14:01 CET 2019


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}}



More information about the R-help mailing list