Currently I have about six or more scripts that do the same job. I thought it might be possible and more efficient to use one script by using IF ELSE statements. Here is an example but this will be expandable for several countries ans year-months Year-month = FEB2015, FEB2012, Feb2010 country = USA, CAN.MEX First I want to do if country = USA and year-month = FEB2015, FEB2012 do the statements second if country = CAN and year-month =Feb2010 do the statements if(country="USA" & year-month = "FEB2015" | "FEB2012" ){ statemnt1 . statemnt10 } else if (country="USA" & year-month ="FEB2015") { statemnt1 . statemnt10 } else { statemnt1 . statemnt10 } The above script did not work. is there a different ways of doing it? Thank you in advance . [[alternative HTML version deleted]]
On 28/02/17 14:47, Val wrote:> Currently I have about six or more scripts that do the same job. I > thought it might be possible and more efficient to use one script by using > IF ELSE statements. Here is an example but this will be expandable for > several countries ans year-months > > > Year-month = FEB2015, FEB2012, Feb2010 > country = USA, CAN.MEX > First I want to do if country = USA and year-month = FEB2015, FEB2012 do > the statements > second if country = CAN and year-month =Feb2010 do the statements > > > if(country="USA" & year-month = "FEB2015" | "FEB2012" ){ > statemnt1 > . > statemnt10 > > } else if (country="USA" & year-month ="FEB2015") { > statemnt1 > . > statemnt10 > } > > else > { > statemnt1 > . > statemnt10 > } > > The above script did not work. is there a different ways of doing it?Uh, yes. Get the syntax right. Use R, when you are using R. Looking at ?Syntax and ?Logic might help you a bit. Other than that, there's not much that one can say without seeing a reproducible example. And if you sat down and wrote out a *reproducible example*, using correct R syntax, you probably wouldn't need any assistance from R-help. Have you read any of the readily available R tutorials? If not do so. If so, read them again and actually take note of what they say! cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
I note that you have "Year-month" (capital 'Y') and "year-month" in your code; case matters in R. Otherwise, Rolf's advice applies. Cheers, Bert 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, Feb 27, 2017 at 6:16 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote:> On 28/02/17 14:47, Val wrote: >> >> Currently I have about six or more scripts that do the same job. I >> thought it might be possible and more efficient to use one script by using >> IF ELSE statements. Here is an example but this will be expandable for >> several countries ans year-months >> >> >> Year-month = FEB2015, FEB2012, Feb2010 >> country = USA, CAN.MEX >> First I want to do if country = USA and year-month = FEB2015, FEB2012 do >> the statements >> second if country = CAN and year-month =Feb2010 do the statements >> >> >> if(country="USA" & year-month = "FEB2015" | "FEB2012" ){ >> statemnt1 >> . >> statemnt10 >> >> } else if (country="USA" & year-month ="FEB2015") { >> statemnt1 >> . >> statemnt10 >> } >> >> else >> { >> statemnt1 >> . >> statemnt10 >> } >> >> The above script did not work. is there a different ways of doing it? > > > Uh, yes. Get the syntax right. Use R, when you are using R. > > Looking at ?Syntax and ?Logic might help you a bit. > > Other than that, there's not much that one can say without seeing a > reproducible example. And if you sat down and wrote out a *reproducible > example*, using correct R syntax, you probably wouldn't need any assistance > from R-help. > > Have you read any of the readily available R tutorials? If not do so. If > so, read them again and actually take note of what they say! > > cheers, > > Rolf Turner > > -- > Technical Editor ANZJS > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > > > ______________________________________________ > R-help at 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.
This is not a reproducible example. You did not even show us one of the scripts that apparently did work. None of your if() statements contains a logical expression. You should read "An Introduction to R" and probably other tutorials on R. https://cran.r-project.org/doc/manuals/r-release/R-intro.html especially https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Logical-vectors and the manual pages at ?Comparison and ?"%in%" ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Val Sent: Monday, February 27, 2017 7:48 PM To: R help <r-help at r-project.org> Subject: [R] if and Currently I have about six or more scripts that do the same job. I thought it might be possible and more efficient to use one script by using IF ELSE statements. Here is an example but this will be expandable for several countries ans year-months Year-month = FEB2015, FEB2012, Feb2010 country = USA, CAN.MEX First I want to do if country = USA and year-month = FEB2015, FEB2012 do the statements second if country = CAN and year-month =Feb2010 do the statements if(country="USA" & year-month = "FEB2015" | "FEB2012" ){ statemnt1 . statemnt10 } else if (country="USA" & year-month ="FEB2015") { statemnt1 . statemnt10 } else { statemnt1 . statemnt10 } The above script did not work. is there a different ways of doing it? Thank you in advance . [[alternative HTML version deleted]] ______________________________________________ R-help at 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.