If/then statement, if in a list then

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
cm
Reply | Threaded
Open this post in threaded view
|

If/then statement, if in a list then

cm
I need to write an if/then statement saying something along the lines of:
if (VALUE is in list) {...
How do I write that in R's language.
Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: If/then statement, if in a list then

John Kane

if(cond) expr

if(cond) cons.expr  else  alt.expr

Also see ?ifelse
John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Mon, 30 Jul 2012 05:55:34 -0700 (PDT)
> To: [hidden email]
> Subject: [R] If/then statement, if in a list then
>
> I need to write an if/then statement saying something along the lines of:
> if (VALUE is in list) {...
> How do I write that in R's language.
> Thanks!
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/If-then-statement-if-in-a-list-then-tp4638346.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: If/then statement, if in a list then

Adams, Jean
In reply to this post by cm
?is.element

Jean


cm <[hidden email]> wrote on 07/30/2012 07:55:34 AM:
>
> I need to write an if/then statement saying something along the lines
of:
> if (VALUE is in list) {...
> How do I write that in R's language.
> Thanks!

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
cm
Reply | Threaded
Open this post in threaded view
|

Re: If/then statement, if in a list then

cm
Thank you so much Jean!
Reply | Threaded
Open this post in threaded view
|

Re: If/then statement, if in a list then

Bert Gunter
In reply to this post by Adams, Jean
Not necessarily. If the OP really meant the R list() structure, then
is.element does not apply. If a vector was what was intended, then it
does -- provide all elements are of the same mode. With such a vague
post, it's hard to know.


-- Bert

On Mon, Jul 30, 2012 at 6:06 AM, Jean V Adams <[hidden email]> wrote:

> ?is.element
>
> Jean
>
>
> cm <[hidden email]> wrote on 07/30/2012 07:55:34 AM:
>>
>> I need to write an if/then statement saying something along the lines
> of:
>> if (VALUE is in list) {...
>> How do I write that in R's language.
>> Thanks!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.



--

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: If/then statement, if in a list then

Michael Weylandt
On Mon, Jul 30, 2012 at 8:49 AM, Bert Gunter <[hidden email]> 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

Best,
Michael

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: If/then statement, if in a list then

Rui Barradas
Hello,

Em 30-07-2012 19:46, R. Michael Weylandt escreveu:

> On Mon, Jul 30, 2012 at 8:49 AM, Bert Gunter <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: If/then statement, if in a list then

Bert Gunter
In reply to this post by Michael Weylandt
??

On Mon, Jul 30, 2012 at 11:46 AM, R. Michael Weylandt
<[hidden email]> wrote:
> On Mon, Jul 30, 2012 at 8:49 AM, Bert Gunter <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: If/then statement, if in a list then

Bert Gunter
Sorry, one error:

On Mon, Jul 30, 2012 at 1:18 PM, Bert Gunter <[hidden email]> wrote:

> ??
>
> On Mon, Jul 30, 2012 at 11:46 AM, R. Michael Weylandt
> <[hidden email]> wrote:
>> On Mon, Jul 30, 2012 at 8:49 AM, Bert Gunter <[hidden email]> 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:
##### Should have been:###############

> is.element(list(letters),x)
[1] 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



--

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

______________________________________________
[hidden email] 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.