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

Rui Barradas ruipbarradas at sapo.pt
Mon Jul 30 21:26:53 CEST 2012


Hello,

Em 30-07-2012 19:46, R. Michael Weylandt escreveu:
> 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))
>
> 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

list(list(letters)) %in% x # Works -- true
identical(letters, x[[6]][[1]]) # True

Rui Barradas

>
> Best,
> Michael
>
> ______________________________________________
> 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.



More information about the R-help mailing list