R match - could be improved ?

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

R match - could be improved ?

Abiologist
​Dear All,

The following gives a very unpleasant experience with apparently random NAs
- probably it's my bad formatting of the coding - but the effect is
unexpected and if undetected can lead to considerable problems:

myvector1 = NULL

myvector3 = NULL

myvector4 = NULL

myvector5 = NULL



myvector1 <- c(1:100)





myvector3[[1]] <- seq(myvector1[1], myvector1["length"(myvector1)], by =
0.01)

myvector4[[1]] <- seq(95, 100, by = 0.01)



myvector5[[1]] <- match(myvector4[[1]], myvector3[[1]])



myvector5



## A solution (but rather annoying):



myvector5[[1]] <- match(as.list(myvector4[[1]]), as.list(myvector3[[1]]))

myvector5

Could anyone tell me why the NAs occur ??

Many thanks.​

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: R match - could be improved ?

Jeff Newmiller
This list is a plain text list. Posting in HTML frequently leads to corrupted code on the receiving end. Be sure to read the Posting Guide.

In no programming language is it a good idea to assume equality with floating point values. See FAQ 7.31. In cases like this use integers to create sequences and then generate floating point from them. Do not go fishing in floating point data for specific values.

Also, you seem to be using list objects for no apparent reason.

myvector1 <- c(1L:100L)

myvector3 <- seq(10L*myvector1[1], 10L*myvector1[ length(myvector1) ])

myvector4 <- seq(950L, 1000L)

myvector5 <- match(myvector4, myvector3)

I think the as.list is working because match is internally converting to character and comparing that... which makes the as.list solution rather inefficient art best.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On March 15, 2015 4:04:29 AM PDT, Jeremy Clark <[hidden email]> wrote:

>​Dear All,
>
>The following gives a very unpleasant experience with apparently random
>NAs
>- probably it's my bad formatting of the coding - but the effect is
>unexpected and if undetected can lead to considerable problems:
>
>myvector1 = NULL
>
>myvector3 = NULL
>
>myvector4 = NULL
>
>myvector5 = NULL
>
>
>
>myvector1 <- c(1:100)
>
>
>
>
>
>myvector3[[1]] <- seq(myvector1[1], myvector1["length"(myvector1)], by
>=
>0.01)
>
>myvector4[[1]] <- seq(95, 100, by = 0.01)
>
>
>
>myvector5[[1]] <- match(myvector4[[1]], myvector3[[1]])
>
>
>
>myvector5
>
>
>
>## A solution (but rather annoying):
>
>
>
>myvector5[[1]] <- match(as.list(myvector4[[1]]),
>as.list(myvector3[[1]]))
>
>myvector5
>​
>Could anyone tell me why the NAs occur ??
>
>Many thanks.​
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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 -- To UNSUBSCRIBE and more, see
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: R match - could be improved ?

J Robertson-Burns
In reply to this post by Abiologist
I believe that you are in Circle 1 of The R Inferno.

http://www.burns-stat.com/documents/books/the-r-inferno/

Pat

On 15/03/2015 11:04, Jeremy Clark wrote:

> ​Dear All,
>
> The following gives a very unpleasant experience with apparently random NAs
> - probably it's my bad formatting of the coding - but the effect is
> unexpected and if undetected can lead to considerable problems:
>
> myvector1 = NULL
>
> myvector3 = NULL
>
> myvector4 = NULL
>
> myvector5 = NULL
>
>
>
> myvector1 <- c(1:100)
>
>
>
>
>
> myvector3[[1]] <- seq(myvector1[1], myvector1["length"(myvector1)], by =
> 0.01)
>
> myvector4[[1]] <- seq(95, 100, by = 0.01)
>
>
>
> myvector5[[1]] <- match(myvector4[[1]], myvector3[[1]])
>
>
>
> myvector5
>
>
>
> ## A solution (but rather annoying):
>
>
>
> myvector5[[1]] <- match(as.list(myvector4[[1]]), as.list(myvector3[[1]]))
>
> myvector5
> ​
> Could anyone tell me why the NAs occur ??
>
> Many thanks.​
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: R match - could be improved ?

Michael Dewey
In reply to this post by Abiologist
Dear Jeremy

The NAs do not seem random to me as the gaps between successive NAs are
either 3, 8 or 11.

Have you considered the possibility that the finite precision of real
numbers in computers may be the issue here?

On 15/03/2015 11:04, Jeremy Clark wrote:

> ​Dear All,
>
> The following gives a very unpleasant experience with apparently random NAs
> - probably it's my bad formatting of the coding - but the effect is
> unexpected and if undetected can lead to considerable problems:
>
> myvector1 = NULL
>
> myvector3 = NULL
>
> myvector4 = NULL
>
> myvector5 = NULL
>
>
>
> myvector1 <- c(1:100)
>
>
>
>
>
> myvector3[[1]] <- seq(myvector1[1], myvector1["length"(myvector1)], by =
> 0.01)
>
> myvector4[[1]] <- seq(95, 100, by = 0.01)
>
>
>
> myvector5[[1]] <- match(myvector4[[1]], myvector3[[1]])
>
>
>
> myvector5
>
>
>
> ## A solution (but rather annoying):
>
>
>
> myvector5[[1]] <- match(as.list(myvector4[[1]]), as.list(myvector3[[1]]))
>
> myvector5
> ​
> Could anyone tell me why the NAs occur ??
>
> Many thanks.​
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 2015.0.5751 / Virus Database: 4306/9307 - Release Date: 03/15/15
>

--
Michael
http://www.dewey.myzen.co.uk

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.