[R] help with if statement with two conditions
Steinmann, Kimberly@CDPR
K|mber|y@Ste|nm@nn @end|ng |rom cdpr@c@@gov
Tue Dec 24 00:38:41 CET 2019
Hi Jim, Burt and all who have been so helpful - very much appreciated!
I have managed to get the code to run correctly by extending the parentheses around the whole statement, including the &, rather than parentheses around the two clauses on either side of the &.
It seems to have done the trick!
I am not sure how to close the thread - I hate to waste anyone's time on a problem that is no longer a problem! I am new to this forum
Thanks again for all the feedback and help everyone!
-Kimberly
-----Original Message-----
From: Jim Lemon <drjimlemon using gmail.com>
Sent: Monday, December 23, 2019 3:33 PM
To: Steinmann, Kimberly using CDPR <Kimberly.Steinmann using cdpr.ca.gov>; r-help mailing list <r-help using r-project.org>
Subject: Re: [R] help with if statement with two conditions
EXTERNAL:
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 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://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=02%7C01%7CKimberly.Ste
> inmann%40cdpr.ca.gov%7C7007357d15244e97a63108d7880088a1%7C0fc528a55d14
> 4d0da7bef2487d8d0e30%7C0%7C1%7C637127408183702102&sdata=x2ugp%2Bs%
> 2BYE%2BmCANwK0QIGtKnfOcHJ0Xpf%2BYZzUTgPxs%3D&reserved=0
> PLEASE do read the posting guide
> https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.R
> -project.org%2Fposting-guide.html&data=02%7C01%7CKimberly.Steinman
> n%40cdpr.ca.gov%7C7007357d15244e97a63108d7880088a1%7C0fc528a55d144d0da
> 7bef2487d8d0e30%7C0%7C1%7C637127408183702102&sdata=sFBbNktlRfA4FZX
> 5qTQdkcNYOx8xNX6MGU2MKnFLoSE%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.
CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure, or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.
More information about the R-help
mailing list