Hi Kimberley, Since you are using a loop and therefore testing one value of v_trends_lbs at a time, the "&" in the "if" statement should be "&&". Pinching Bert's example but using a for loop instead of ifelse:
x <- seq(-2,2,.25) v_lbs<-rep("",length(x)) for(i in 1:length(x)) { if(is.na(x[i])) v_lbs<-"0" else { if(x[i] < 0.51 && x[i] > 0) v_lbs[i]<-"<1" else v_lbs[i] <- format(round(x[i]), big.mark=",",scientific=FALSE) } } x v_lbs I think this gives you the "right" answer, as I have used 0.51 to avoid the rounding problem at 0.5. I can see that the output is feeding into something else, and an incorrect value at the end of your calculations would be more difficult to track back. Bert's "ifelse" is a more elegant way to do this, although not quote as easy for the beginner to understand. Jim On Tue, Dec 24, 2019 at 7:33 AM Steinmann, Kimberly@CDPR <kimberly.steinm...@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@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. ______________________________________________ R-help@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.