[R] Logical operators and named arguments
Ryan
reckbo at bwh.harvard.edu
Thu Aug 7 08:35:13 CEST 2014
Josh,
Thank you for your detailed answer.
Best,
Ryan
On 7 Aug 2014, at 16: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 TRUE
>
> From ?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 TRUE
>> sapply(1:3, function(x) `>`(2, x))
> [1] TRUE FALSE FALSE
>
>
> HTH,
>
> Josh
>
>
>
> On Thu, Aug 7, 2014 at 2:20 PM, Ryan <reckbo at 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 t...{{dropped:28}}
More information about the R-help
mailing list