Hi all, I have two questions. Can some one give some help? The first question is regarding the pair of operators "&" and "&&". What is the difference between the two? The second question is regarding "<-" and "=". Usually we use "<-" as the assignment operator. I saw some people use "=". Is there any difference between the two. Thank you!! Hannah [[alternative HTML version deleted]]
On Jun 16, 2010, at 6:33 PM, li li wrote:> Hi all, > I have two questions. Can some one give some help? > > The first question is regarding the pair of operators "&" and > "&&". What > is the > difference between the two?& operates on vectors, returns a vector && operates on the first item in each argument. Retruns a value and a warning if either of the arguments of of length > 1.> > The second question is regarding "<-" and "=". Usually we use > "<-" as the assignment operator. I saw some people use "=". Is there > any difference between the two. >Yes. Many discussions in the archives. will be more erudite and accurate than mine. "=" is preferred for assigning values in argument lists. "<-" is preferred for assignment to objects. Generally the differences do not matter but there are exceptions. -- David.
Just to expand a little on David's reply. The & vs. && and | vs. || issue is really about where and how you plan to use things. & and | work on vectors and are intended to be used to combine logical vectors into a new logical vector (that can be used for various things). && and || are used for program control, mainly in the condition of if or while statements. The program flow versions have the benefit of evaluating the left condition, then only evaluating the right condition if needed (this can save some warning messages and time). Compare the following commands:> x <- rnorm(100) > any(x < 0) | any(log(x) < 0) > any(x < 0) || any(log(x) < 0)The '<-' operator is for assignment, the '=' is used to match formal arguments in functions to their values. In some cases where it is unambiguous the '=' can be used in place of '<-' (see the help page). But you need to understand the difference since there are cases where they will not do the same thing.> mean( x <- rnorm(100) )And> mean( x = rnorm(100) )Do not do the same thing (well part is the same, but there is a subtle but significant difference).> mean( z <- rnorm(100) )And> mean( z = rnorm(100) )Are even more different. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of li li > Sent: Wednesday, June 16, 2010 4:33 PM > To: r-help > Subject: [R] questions on some operators in R > > Hi all, > I have two questions. Can some one give some help? > > The first question is regarding the pair of operators "&" and "&&". > What > is the > difference between the two? > > The second question is regarding "<-" and "=". Usually we use > "<-" as the assignment operator. I saw some people use "=". Is there > any difference between the two. > > Thank you!! > Hannah > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Greg, your second example, recording the run time of an operation or a function, would make the use of '=' problematic. But I wonder if that's specific to system.time. H -----Original Message----- From: Greg Snow [mailto:Greg.Snow at imail.org] Sent: Friday, June 18, 2010 3:33 PM To: Horace Tso Subject: RE: [R] questions on some operators in R And the 2nd example? -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: Horace Tso [mailto:Horace.Tso at pgn.com] > Sent: Friday, June 18, 2010 3:09 PM > To: Greg Snow > Subject: RE: [R] questions on some operators in R > > Then, break it into two lines, > > x = rnorm(100) > mean(x) > > H > > -----Original Message----- > From: Greg Snow [mailto:Greg.Snow at imail.org] > Sent: Friday, June 18, 2010 1:16 PM > To: Horace Tso; li li > Cc: r-help > Subject: RE: [R] questions on some operators in R > > Your example could also be used as an argument against allowing '=' as > a shortcut for <- after all if you are used to using <- (rather than =) > then you will see the problem with x<-2 right off. But if we eliminate > <- and only use =, then how do you do: > > > mean( x <- rnorm(100) ) > > Or > > > system.time( output <- longrunningfunction(args) ) > > Is > > > mean( { x=rnorm(100) } ) > > Really and improvement? > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.snow at imail.org > 801.408.8111 > > > > -----Original Message----- > > From: Horace Tso [mailto:Horace.Tso at pgn.com] > > Sent: Friday, June 18, 2010 12:16 PM > > To: li li; Greg Snow > > Cc: r-help > > Subject: RE: [R] questions on some operators in R > > > > Li li, > > > > I know many S-language old timers would tell you to use <- over = for > > assignment. Speaking from my own painful experience of debugging S/R > > codes, I much much much prefer '='. In fact, I'd like to see the R > > language get ride of '<-' as the assignment operator. > > > > Here is why. > > > > > x = -5:10 > > > x > > [1] -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 > > > > Now I want to find elements of x which are smaller than negative 2, > or > > -2. So naturally I'd do, > > > > > which(x<-2) > > Error in which(x <- 2) : argument to 'which' is not logical > > > > Oops, what happened? If you look up help pages for 'which', you'd > find > > no clue. > > > > What occurred in the parenthesis is that you've overidden your vector > x > > with a single value of 2, thanks to the assignment operator '<-'. > > > > This' a big problem not just because you might end up spending hours > > finding out what's wrong with such innocent expression. The worst > part > > is, you'd have lost your vector x forever. Just image if x is 1200 by > > 1200 matrix. > > > > HTH. > > > > H > > > > > > > > > > > > -----Original Message----- > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > > project.org] On Behalf Of li li > > Sent: Friday, June 18, 2010 10:01 AM > > To: Greg Snow > > Cc: r-help > > Subject: Re: [R] questions on some operators in R > > > > Thank you all for your kind reply! > > Hannah > > > > 2010/6/18 Greg Snow <Greg.Snow at imail.org> > > > > > Just to expand a little on David's reply. > > > > > > The & vs. && and | vs. || issue is really about where and how you > > plan to > > > use things. & and | work on vectors and are intended to be used to > > combine > > > logical vectors into a new logical vector (that can be used for > > various > > > things). && and || are used for program control, mainly in the > > condition of > > > if or while statements. The program flow versions have the benefit > > of > > > evaluating the left condition, then only evaluating the right > > condition if > > > needed (this can save some warning messages and time). Compare the > > > following commands: > > > > > > > x <- rnorm(100) > > > > any(x < 0) | any(log(x) < 0) > > > > any(x < 0) || any(log(x) < 0) > > > > > > > > > The '<-' operator is for assignment, the '=' is used to match > formal > > > arguments in functions to their values. In some cases where it is > > > unambiguous the '=' can be used in place of '<-' (see the help > page). > > But > > > you need to understand the difference since there are cases where > > they will > > > not do the same thing. > > > > > > > mean( x <- rnorm(100) ) > > > And > > > > mean( x = rnorm(100) ) > > > > > > Do not do the same thing (well part is the same, but there is a > > subtle but > > > significant difference). > > > > > > > mean( z <- rnorm(100) ) > > > And > > > > mean( z = rnorm(100) ) > > > > > > Are even more different. > > > > > > > > > > > > -- > > > Gregory (Greg) L. Snow Ph.D. > > > Statistical Data Center > > > Intermountain Healthcare > > > greg.snow at imail.org > > > 801.408.8111 > > > > > > > > > > -----Original Message----- > > > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > > > > project.org] On Behalf Of li li > > > > Sent: Wednesday, June 16, 2010 4:33 PM > > > > To: r-help > > > > Subject: [R] questions on some operators in R > > > > > > > > Hi all, > > > > I have two questions. Can some one give some help? > > > > > > > > The first question is regarding the pair of operators "&" and > > "&&". > > > > What > > > > is the > > > > difference between the two? > > > > > > > > The second question is regarding "<-" and "=". Usually we use > > > > "<-" as the assignment operator. I saw some people use "=". Is > > there > > > > any difference between the two. > > > > > > > > Thank you!! > > > > Hannah > > > > > > > > [[alternative HTML version deleted]] > > > > > > > > ______________________________________________ > > > > R-help at r-project.org mailing list > > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > > PLEASE do read the posting guide http://www.R- > project.org/posting- > > <http://www.r-project.org/posting-> > > > > guide.html > > > > and provide commented, minimal, self-contained, reproducible > code. > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list > > 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.
But one could argue that <= could also mean assignment (although as a mathematician I'd go with implies or perhaps 'is implied by') and wouldn't have the problem highlighted below. Similarly one could use the Pascal := for assignment. So although the idea of having two different operators for 'is equal to' and 'make equal to' makes sense what we have today isn't neccesarily right - we got rid of '_' as assignment for example. -------------------------- David Jessop Global Head of Quantitative Research UBS Investment Research +44 20 7567 9882 ----- Original Message ----- From: r-help-bounces at r-project.org <r-help-bounces at r-project.org> To: Horace Tso <Horace.Tso at pgn.com> Cc: r-help <r-help at r-project.org> Sent: Fri Jun 18 23:26:23 2010 Subject: Re: [R] questions on some operators in R On Fri, Jun 18, 2010 at 2:15 PM, Horace Tso <Horace.Tso at pgn.com> wrote:> Li li, > > I know many S-language old timers would tell you to use <- over = for assignment. Speaking from my own painful experience of debugging S/R codes, I much much much prefer '='. In fact, I'd like to see the R language get ride of '<-' as the assignment operator. > > Here is why. > >> x = -5:10 >> x > ?[1] -5 -4 -3 -2 -1 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 10 > > Now I want to find elements of x which are smaller than negative 2, or -2. So naturally I'd do, > >> which(x<-2) > Error in which(x <- 2) : argument to 'which' is not logical > > Oops, what happened? If you look up help pages for 'which', you'd find no clue. > > What occurred in the parenthesis is that you've overidden your vector x with a single value of 2, thanks to the assignment operator '<-'. > > This' a big problem not just because you might end up spending hours finding out what's wrong with such innocent expression. The worst part is, you'd have lost your vector x forever. Just image if x is 1200 by 1200 matrix. >I prefer <- because it makes more sense. It evokes the idea of assignment, which is what it is, whereas = reminds one of equality which is wrong since its not equality but assignment. Its also consistent with <<- whereas it seems highly inconsistent to use = and <<- . Its also less ambiguous when not used on the left hand side of a statement. ______________________________________________ R-help at r-project.org mailing list 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. -------------- next part -------------- Issued by UBS AG or affiliates to professional investors for information only and its accuracy/completeness is not guaranteed. All opinions may change without notice and may differ to opinions/recommendations expressed by other business areas of UBS. UBS may maintain long/short positions and trade in instruments referred to. Unless stated otherwise, this is not a personal recommendation, offer or solicitation to buy/sell and any prices/quotations are indicative only. UBS may provide investment banking and other services to, and/or its employees may be directors of, companies referred to. To the extent permitted by law, UBS does not accept any liability arising from the use of this communication. \251 UBS 2010. All rights reserved. Intended for recipient only and not for further distribution without the consent of UBS. UBS Limited is a company registered in England & Wales under company number 2035362, whose registered office is at 1 Finsbury Avenue, London, EC2M 2PP, United Kingdom. UBS AG (London Branch) is registered as a branch of a foreign company under number BR004507, whose registered office is at 1 Finsbury Avenue, London, EC2M 2PP, United Kingdom. UBS Clearing and Execution Services Limited is a company registered in England & Wales under company number 03123037, whose registered office is at 1 Finsbury Avenue, London, EC2M 2PP, United Kingdom. UBS reserves the right to retain all messages. Messages are protected and accessed only in legally justified cases.