how to not match partial names

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

how to not match partial names

William Revelle
Dear friends,

How do I stop partial matching of list names?

e.g.,

x <- list(AAAA="aaaaa", BBBBB="bbbbb")
is.null(x$A)   #returns FALSE  even though there is no element A.

if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
result   #is aaaa even though there is no x$A element

x <- list(CCCC="aaaaa", BBBBB="bbbbb")
if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
result   #this is great

x <- list(ABC="aaaaa", BBBBB="bbbbb")
if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
result  #partial matches  and returns aaaa

x <- list(ABC="abc", BBBBB="bbbbb",AA="aaaa")
if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
result  #can not partial match, and thus returns bbbb

x <- list(AAB="aab", BBBBB="bbbbb",AA="aaaa")
if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
result #also can not partial match

My need for this is that I have several functions that return lists
and I am trying to extract AAAA if it exists, but something else if
it does not.

Thanks.

Bill

--
William Revelle http://personality-project.org/revelle.html
Professor http://personality-project.org
Department of Psychology             http://www.wcas.northwestern.edu/psych/
Northwestern University http://www.northwestern.edu/
Use R for psychology                       http://personality-project.org/r
It is 6 minutes to midnight http://www.thebulletin.org

______________________________________________
[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: how to not match partial names

Thomas Lumley-2
On Sun, May 8, 2011 at 11:09 AM, William Revelle <[hidden email]> wrote:

> Dear friends,
>
> How do I stop partial matching of list names?
>
> e.g.,
>
> x <- list(AAAA="aaaaa", BBBBB="bbbbb")
> is.null(x$A)   #returns FALSE  even though there is no element A.
>
> if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
> result   #is aaaa even though there is no x$A element
>

x[["A"]]

   -thomas

--
Thomas Lumley
Professor of Biostatistics
University of Auckland

______________________________________________
[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: how to not match partial names

Duncan Murdoch-2
In reply to this post by William Revelle
On 07/05/2011 7:09 PM, William Revelle wrote:

> Dear friends,
>
> How do I stop partial matching of list names?
>
> e.g.,
>
> x<- list(AAAA="aaaaa", BBBBB="bbbbb")
> is.null(x$A)   #returns FALSE  even though there is no element A.
>
> if(is.null(x$A))  {result<-  x$BBBB} else {result<- x$A}
> result   #is aaaa even though there is no x$A element
>
> x<- list(CCCC="aaaaa", BBBBB="bbbbb")
> if(is.null(x$A))  {result<-  x$BBBB} else {result<- x$A}
> result   #this is great
>
> x<- list(ABC="aaaaa", BBBBB="bbbbb")
> if(is.null(x$A))  {result<-  x$BBBB} else {result<- x$A}
> result  #partial matches  and returns aaaa
>
> x<- list(ABC="abc", BBBBB="bbbbb",AA="aaaa")
> if(is.null(x$A))  {result<-  x$BBBB} else {result<- x$A}
> result  #can not partial match, and thus returns bbbb
>
> x<- list(AAB="aab", BBBBB="bbbbb",AA="aaaa")
> if(is.null(x$A))  {result<-  x$BBBB} else {result<- x$A}
> result #also can not partial match
>
> My need for this is that I have several functions that return lists
> and I am trying to extract AAAA if it exists, but something else if
> it does not.

Both

"A" %in% names(x)

will return false, and

is.null(x[["A"]])

will return TRUE.

Duncan Murdoch

______________________________________________
[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: how to not match partial names

"Dénes TÓTH"
In reply to this post by William Revelle
> Dear friends,
>
> How do I stop partial matching of list names?
>
> e.g.,
>
> x <- list(AAAA="aaaaa", BBBBB="bbbbb")
> is.null(x$A)   #returns FALSE  even though there is no element A.
>
> if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
> result   #is aaaa even though there is no x$A element
>
> x <- list(CCCC="aaaaa", BBBBB="bbbbb")
> if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
> result   #this is great
>
> x <- list(ABC="aaaaa", BBBBB="bbbbb")
> if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
> result  #partial matches  and returns aaaa
>
> x <- list(ABC="abc", BBBBB="bbbbb",AA="aaaa")
> if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
> result  #can not partial match, and thus returns bbbb
>
> x <- list(AAB="aab", BBBBB="bbbbb",AA="aaaa")
> if(is.null(x$A))  {result <-  x$BBBB} else {result <- x$A}
> result #also can not partial match
>
> My need for this is that I have several functions that return lists
> and I am trying to extract AAAA if it exists, but something else if
> it does not.


ifelse("AAAA" %in% names(x),x[["AAAA"]],x[["BBBB"]])


HTH,
  Denes


>
> Thanks.
>
> Bill
>
> --
> William Revelle http://personality-project.org/revelle.html
> Professor http://personality-project.org
> Department of Psychology
> http://www.wcas.northwestern.edu/psych/
> Northwestern University http://www.northwestern.edu/
> Use R for psychology
> http://personality-project.org/r
> It is 6 minutes to midnight http://www.thebulletin.org
>
> ______________________________________________
> [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.