help with if statement with two conditions

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

help with if statement with two conditions

ksteinmann
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: help with if statement with two conditions

Patrick (Malone Quantitative)
What does the error message say?

On Mon, Dec 23, 2019, 3:33 PM Steinmann, Kimberly@CDPR <
[hidden email]> 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}}
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with if statement with two conditions

Bert Gunter-2
In reply to this post by ksteinmann
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@CDPR <
[hidden email]> 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}}
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with if statement with two conditions

Jim Lemon-4
In reply to this post by ksteinmann
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
<[hidden email]> 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}}
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with if statement with two conditions

Jim Lemon-4
Hi Kimberley,
Given the number of posts that read "I have a problem, please advise",
your concern for our mental welfare is a great Xmas present.

Jim

On Tue, Dec 24, 2019 at 10:38 AM Steinmann, Kimberly@CDPR
<[hidden email]> wrote:
>
> 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
>

______________________________________________
[hidden email] 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.