[R] Unexpected behaviour with seq() and %in%
arun
smartpink111 at yahoo.com
Fri Aug 23 21:31:15 CEST 2013
Hi,
Possibly R FAQ 7.31
vec1[180]
#[1] 18
vec1[180]-18
#[1] 3.552714e-15
vec1[round(vec1,2)%in%vec2]
# [1] 2 4 6 8 10 12 14 16 18 20
A.K.
----- Original Message -----
From: Finlay Scott <drfinlayscott at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, August 23, 2013 11:00 AM
Subject: [R] Unexpected behaviour with seq() and %in%
Hi,
During the course of putting together a function I came across some
unexpected behaviour when using seq() and %in%.
I am creating two numeric vectors using seq(), and then using %in% to find
the values in one vector that are in the other. Sometimes all the values
are found, but sometimes a value is not found. It seems that the behaviour
is inconsistent and I don't understand why.
Below are some reproducible examples:
vec1 <- seq(from = 0.1, to = 20, by = 0.1)
vec2 <- seq(from = 2, to = 20, by = 2)
vec1[vec1 %in% vec2]
[1] 2 4 6 8 10 12 14 16 20
The value 18 is not found.
Starting vec1 at 0.2:
vec1 <- seq(from = 0.2, to = 20, by = 0.1)
vec2 <- seq(from = 2, to = 20, by = 2)
vec1[vec1 %in% vec2]
[1] 2 4 8 10 12 14 16 18 20
Now the value 6 is not found.
Starting vec1 at 0.5:
vec1 <- seq(from = 0.5, to = 20, by = 0.1)
vec2 <- seq(from = 2, to = 20, by = 2)
vec1[vec1 %in% vec2]
[1] 2 4 6 8 10 12 14 16 18 20
Now all the values are found.
Can someone please explain what is happening? Why should the starting value
of vec1 make a difference?
Thanks
Finlay
R version info:
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
version.string R version 3.0.1 (2013-05-16)
[[alternative HTML version deleted]]
______________________________________________
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