[R] If/then statement, if in a list then

Bert Gunter gunter.berton at gene.com
Mon Jul 30 22:18:41 CEST 2012


??

On Mon, Jul 30, 2012 at 11:46 AM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
> On Mon, Jul 30, 2012 at 8:49 AM, Bert Gunter <gunter.berton at gene.com> wrote:
>> Not necessarily. If the OP really meant the R list() structure, then
>> is.element does not apply.
>
> Perhaps...
>
> x <- list(1:5, "a", `+`, rnorm, NULL, list(letters))

Note:
> is.element((1:5),x)
[1] FALSE FALSE FALSE FALSE FALSE

##The answer should be TRUE -- the vector (1:5) is a list component.

Similarly:
> is.element(letters,x)
 [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE

## The answer again should be TRUE.

> is.element(rnorm,x)
Error in match(el, set, 0L) : 'match' requires vector arguments

## The answer should be TRUE.

So I do not understand what your point is. I stand by my claim:
is.element is not intended for lists, and this is made clear (to me,
anyway) in the help file.

-- Bert






>
> letters %in% x # Works -- vectorized, mostly false, but the "a" is
> there, per below
> "a" %in% x # Works, true
>
> 1 %in% x # Works, false
> 1:5 %in% x # Works -- vectorized, false
> list(1:5) %in% x # Works, true
>
> `+` %in% x # Error
> NULL %in% x # logical(0)
>
> so it seems is.element / %in% [chacun son gout] works with "vectors"
> (in a rather broad sense of that word)
>
> I'm still trying to understand quite how this one works though:
>
> list(letters) %in% x # Works -- false: this one surprised me!
> identical(list(letters), x[[6]]) # True
>
> Best,
> Michael



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list