[R] Which() missing a number

Duncan Murdoch murdoch.duncan at gmail.com
Tue Mar 18 13:22:48 CET 2014


On 18/03/2014 7:14 AM, Thomas wrote:
> Does anyone know why this is happening? Which() is picking up the
> indices of the numbers 13 and 15 in neilist, but it's missing out the
> 13 at index 6.
>
> Thank you,
>
> Thomas Chesney
>
>   > neilist
>    [1] 13 15 28 29 30 13 14 15 28 30 43 44 45 14 15 29 44 45
>
>   > pfriends
> [1] 13 15
>
>   > which(neilist==pfriends)
> [1] 1 2 8

Two problems with your code.  The first is recycling.  Your expression 
neilist == pfriends compares neilist[1] with pfriends[1], then 
neilist[2] with pfriends[2], then neilist[3] with pfriends[1], etc.

The second problem is that pfriends is not an integer vector, so it may 
actually contain 13.00000000001 and 14.9999999999
(or similar numbers that round to 13 and 15 when printed), and those 
won't test equal to 13 and 15.

Duncan Murodch
>
>   > neilist[6]
> [1] 13
>
>   > str(neilist)
>    int [1:18] 13 15 28 29 30 13 14 15 28 30 ...
>
>   > str(pfriends)
>    num [1:2] 13 15
>
>   > sessionInfo()
> R version 2.15.3 (2013-03-01)
> Platform: i386-apple-darwin9.8.0/i386 (32-bit)
>
> locale:
> [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
>
> attached base packages:
> [1] splines   grid      stats     graphics  grDevices utils
> datasets  methods
> [9] base
>
> other attached packages:
>    [1] spdep_0.5-56    coda_0.16-1     deldir_0.0-21   maptools_0.8-23
> foreign_0.8-52
>    [6] nlme_3.1-108    MASS_7.3-23     Matrix_1.0-11   lattice_0.20-13
> boot_1.3-7
> [11] sp_1.0-8        igraph_0.6.5-1
>
> loaded via a namespace (and not attached):
> [1] LearnBayes_2.12 tools_2.15.3
>    
> This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it.   Please do not use, copy or disclose the information contained in this message or in any attachment.  Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.
>
> This message has been checked for viruses but the contents of an attachment
> may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
>
> ______________________________________________
> 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