Hi friends, I am aware of the function "-"() which acts as minus in ordinary computations. For example:> "-"(3, 1)[1] 2 However what is the meaning of> "-"(3)[1] -3 I was expecting R to generate some error as it does for "*"(3). What is the logic for that calculation? Thanks, [[alternative HTML version deleted]]
well "*"(3,2) works but "*"(3,2,3) does not. You should now be able to figure out the logic. It is related to the number of arguments that make sense. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.list at gmail.com On Aug 1, 2010, at 10:56 AM, Ron Michael wrote:> Hi friends, I am aware of the function "-"() which acts as minus in > ordinary computations. For example: > >> "-"(3, 1) > [1] 2 > > However what is the meaning of >> "-"(3) > [1] -3 > > I was expecting R to generate some error as it does for "*"(3). What > is the logic for that calculation? > > Thanks, > > > [[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.
On Sun, Aug 1, 2010 at 7:56 AM, Ron Michael <ron_michael70 at yahoo.com> wrote:> Hi friends, I am aware of the function "-"() which acts as minus in ordinary computations. For example: > >> "-"(3, 1) > [1] 2 > > However what is the meaning of >> "-"(3) > [1] -3This seems quite functionally useful; consider the behavior of some of the other operators:> - 3[1] -3> --3[1] 3> +3[1] 3> /3Error: unexpected '/' in "/"> *3Error: unexpected '*' in "*" '-' and '+' taking one argument is rather convenient.> > I was expecting R to generate some error as it does for "*"(3). What is the logic for that calculation? > > Thanks, > > > ? ? ? ?[[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. > >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Ron, In arithmetic, '-' and '+' are binary _and_ unary operators. That is, both -1 and 1-1 are valid arithmetic expressions, the former negates its argument, and the latter subtracts the second from the first. Since much of R is designed do arithmetic, R honors the unary _and_ binary versions of '-' and '+'. The implementation of `-`() performs negation when the second argument is missing, and subtraction when both arguments are present. AFAIR, the only other unary (but never binary) operator in R is '!', or the 'NOT' operator (maybe also the one-sided formula operator '~'). In contrast, the 'times' or 'multiply' operator '*' is generally a binary operator in arithmetic. Hence, the function `*`() requires two arguments. -Matt On Sun, 2010-08-01 at 10:56 -0400, Ron Michael wrote:> Hi friends, I am aware of the function "-"() which acts as minus in ordinary computations. For example: > > > "-"(3, 1) > [1] 2 > > However what is the meaning of > > "-"(3) > [1] -3 > > I was expecting R to generate some error as it does for "*"(3). What is the logic for that calculation? > > Thanks, > > > [[alternative HTML version deleted]] >-- Matthew S. Shotwell Graduate Student Division of Biostatistics and Epidemiology Medical University of South Carolina