On 09/08/2014 01:10, Joshua Wiley wrote:
On Sat, Aug 9, 2014 at 9:56 AM, Patrick Burns <pbu...@pburns.seanet.com> wrote:On 07/08/2014 07:21, Joshua Wiley wrote:Hi Ryan, It does work, but the *apply family of functions always pass to the first argument, so you can specify e2 = , but not e1 =. For example: sapply(1:3, `>`, e2 = 2)[1] FALSE FALSE TRUEThat is not true:But it is passed as the first argument, not by name, but positionally. The reason it works with your gt() is because R with regular functions is flexible:f <- function(x, y) x > y f(1:3, x = 2)[1] TRUE FALSE FALSE but primitives ARE positionally matched
That's not true either. Almost all primitives intended to be called as functions do have standard argument-matching semantics. (Once upon a time they did not, but I added the requisite code years ago.) There are six exceptions plus binary operators and other language elements.
See http://cran.r-project.org/doc/manuals/r-release/R-ints.html#g_t_002eInternal-vs-_002ePrimitive and the comments about primitive functions in ?lapply.
`>`(1:3, 2)[1] FALSE FALSE TRUE`>`(1:3, e1 = 2)[1] FALSE FALSE TRUEgt <- function(x, y) x > ysapply(1:3, gt, y=2)[1] FALSE FALSE TRUEsapply(1:3, gt, x=2)[1] TRUE FALSE FALSE Specifying the first argument(s) in an apply call is a standard way of getting flexibility. I'd hazard to guess that the reason the original version doesn't work is because `>` is Primitive. There's speed at the expense of not behaving quite the same as typical functions. PatFrom ?sapply'lapply' returns a list of the same length as 'X', each element of which is the result of applying 'FUN' to the corresponding element of 'X'. so `>` is applied to each element of 1:3 `>`(1, ...) `>`(2, ...) `>`(3, ...) and if e2 is specified than that is passed `>`(1, 2) `>`(2, 2) `>`(3, 2) Further, see ?Ops If the members of this group are called as functions, any argument names are removed to ensure that positional matching is always used. and you can see this at work: `>`(e1 = 1, e2 = 2)[1] FALSE`>`(e2 = 1, e1 = 2)[1] FALSE If you want to the flexibility to specify which argument the elements of X should be *applied to, use a wrapper: sapply(1:3, function(x) `>`(x, 2))[1] FALSE FALSE TRUEsapply(1:3, function(x) `>`(2, x))[1] TRUE FALSE FALSE HTH, Josh On Thu, Aug 7, 2014 at 2:20 PM, Ryan <rec...@bwh.harvard.edu> wrote: Hi,I'm wondering why calling ">" with named arguments doesn't work as expected: args(">")function (e1, e2) NULL sapply(c(1,2,3), `>`, e2=0)[1] TRUE TRUE TRUE sapply(c(1,2,3), `>`, e1=0)[1] TRUE TRUE TRUE Shouldn't the latter be FALSE? Thanks for any help, Ryan The information in this e-mail is intended only for th...{{dropped:23}}______________________________________________ R-help@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.-- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming')
-- Brian D. Ripley, rip...@stats.ox.ac.uk Emeritus Professor of Applied Statistics, University of Oxford 1 South Parks Road, Oxford OX1 3TG, UK ______________________________________________ R-help@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.