Skip to content
Back to formatted view

Raw Message

Message-ID: <b736f9e8-9ae1-4fce-8531-eec3488d1cd5@syonic.eu>
Date: 2023-10-21T20:04:13Z
From: Leonard Mada
Subject: Issue from R-devel: subset on table
In-Reply-To: <142f3b97-f9c5-4f92-a5fb-0d12cc5d30e9@syonic.eu>

Another solution could also be possible - see below.


On 10/21/2023 10:38 PM, Leonard Mada wrote:
> My mistake!
>
> It does actually something else, which is incorrect. One could still 
> use (although the code is more difficult to read):
>
> subset(tmp <- table(sample(1:10, 100, T)), tmp > 10)

2) Alternative solution
Enhance subset.default to accept also formulas, e.g.:

subset.default = function (x, subset, ...)
{
 ?? ?if(inherits(subset, "formula")) {
 ?? ???? subset = subset[[2]];
 ?? ???? subset = eval(subset, list("." = x));
 ?? ?} else if (! is.logical(subset))
 ?? ???? stop("'subset' must be logical")
 ?? ?x[subset & ! is.na(subset)]
}

# it works now: but results depend on sample()
subset(table(sample(1:10, 100, T)), ~ . > 10)
subset(table(sample(1:10, 100, T)), ~ . > 10 & . < 13)


>
>
> Sincerely,
>
>
> Leonard
>
>
> On 10/21/2023 10:26 PM, Leonard Mada wrote:
>> Dear List Members,
>>
>> There was recently an issue on R-devel (which I noticed only very late):
>> https://stat.ethz.ch/pipermail/r-devel/2023-October/082943.html
>>
>> It is possible to use subset as well, almost as initially stated:
>>
>> subset(table(sample(1:5, 100, T)), table > 10)
>> # Error in table > 10 :
>> #? comparison (>) is possible only for atomic and list types
>>
>> subset(table(sample(1:5, 100, T)), 'table' > 10)
>> #? 1? 2? 3? 4? 5
>> # 21 13 15 28 23
>
>
> Note: The result was ok only by chance! But it is incorrect in general.
>
>
>>
>> Works with the letters-example as well.
>>
>> Sincerely,
>>
>> Leonard
>>
>>