Hello,
I came across a strange behavior of expand.grid (or at least strange to me).
For certain values of one of my input variables - created by seq() - I
have to use strings (e.g ==".6") to select a row of the object
created by expand.grid(), for other values numerical (e.g. ==.8) and
for some both work. Please find an example below.
#Example
x<- seq(0,1,1/10)
y <- seq(0,1,1/10)
gridd <- expand.grid(x,y )
gridd[which( gridd[,2]=="0.6" ),] #gives me the right data
gridd[which( gridd[,2]==0.6 ) ,] #gives error message
gridd[which( gridd[,2]=="0.4" ),] #gives right data
gridd[which( gridd[,2]==0.4 ),] #gives right data
gridd[which( gridd[,2]=="0.0" ),] #gives error message
gridd[which( gridd[,2]==0.0 ),] #gives me the right data
#End of Example
I also encountered this phenomenon on another version of R and on
another computer.
Has anybody an idea what this is and how to overcome it?
Best,
Stefan
Sys.getlocale()
[1] "LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
Kingdom.1252;LC_MONETARY=English_United
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252"
version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 2
minor 8.0
year 2008
month 10
day 20
svn rev 46754
language R
version.string R version 2.8.0 (2008-10-20)
strange (?) behavoir of expand.grid()
5 messages · Karl Ove Hufthammer, David Winsemius, Baptiste Auguie +1 more
stefan.duke at gmail.com:
I came across a strange behavior of expand.grid (or at least strange to me). For certain values of one of my input variables - created by seq() - I have to use strings (e.g ==".6") to select a row of the object created by expand.grid(), for other values numerical (e.g. ==.8) and for some both work.
It?s not a bug. Please see http://wiki.r-project.org/rwiki/doku.php?id=misc:r_accuracy&s=floating and the documents referenced therein.
Karl Ove Hufthammer
http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f -- David Winsemius
On Apr 7, 2009, at 10:35 AM, stefan.duke at gmail.com wrote:
Hello,
I came across a strange behavior of expand.grid (or at least strange
to me).
For certain values of one of my input variables - created by seq() - I
have to use strings (e.g ==".6") to select a row of the object
created by expand.grid(), for other values numerical (e.g. ==.8) and
for some both work. Please find an example below.
#Example
x<- seq(0,1,1/10)
y <- seq(0,1,1/10)
gridd <- expand.grid(x,y )
gridd[which( gridd[,2]=="0.6" ),] #gives me the right data
gridd[which( gridd[,2]==0.6 ) ,] #gives error message
gridd[which( gridd[,2]=="0.4" ),] #gives right data
gridd[which( gridd[,2]==0.4 ),] #gives right data
gridd[which( gridd[,2]=="0.0" ),] #gives error message
gridd[which( gridd[,2]==0.0 ),] #gives me the right data
#End of Example
I also encountered this phenomenon on another version of R and on
another computer.
Has anybody an idea what this is and how to overcome it?
Best,
Stefan
Sys.getlocale()
[1] "LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
Kingdom.1252;LC_MONETARY=English_United
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252"
version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 2
minor 8.0
year 2008
month 10
day 20
svn rev 46754
language R
version.string R version 2.8.0 (2008-10-20)
Hi, I think it's a FAQ (== vs all.equal to test for equality), and not related to expand.grid. See ?all.equal and ?"==" You don't need which, gridd[gridd[,2]=="0.6" , ] would work fine, or more elegantly (imho),
gridd <- expand.grid(x=x,y=y )
subset(gridd, factor(x) == "0.6")
Hope this helps, baptiste
On 7 Apr 2009, at 15:35, stefan.duke at gmail.com wrote:
Hello,
I came across a strange behavior of expand.grid (or at least strange
to me).
For certain values of one of my input variables - created by seq() - I
have to use strings (e.g ==".6") to select a row of the object
created by expand.grid(), for other values numerical (e.g. ==.8) and
for some both work. Please find an example below.
#Example
x<- seq(0,1,1/10)
y <- seq(0,1,1/10)
gridd <- expand.grid(x,y )
gridd[which( gridd[,2]=="0.6" ),] #gives me the right data
gridd[which( gridd[,2]==0.6 ) ,] #gives error message
gridd[which( gridd[,2]=="0.4" ),] #gives right data
gridd[which( gridd[,2]==0.4 ),] #gives right data
gridd[which( gridd[,2]=="0.0" ),] #gives error message
gridd[which( gridd[,2]==0.0 ),] #gives me the right data
#End of Example
I also encountered this phenomenon on another version of R and on
another computer.
Has anybody an idea what this is and how to overcome it?
Best,
Stefan
Sys.getlocale()
[1] "LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
Kingdom.1252;LC_MONETARY=English_United
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252"
version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 2
minor 8.0
year 2008
month 10
day 20
svn rev 46754
language R
version.string R version 2.8.0 (2008-10-20)
______________________________________________ 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.
_____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
Hi ya'll, thanks for pointing out the obvious ;-) I didn't think of that (and I showed it to a fellow colleague who didn't spot it either, I am going to blame her for that). Best, Stefan On Tue, Apr 7, 2009 at 4:52 PM, Karl Ove Hufthammer
<Karl.Hufthammer at math.uib.no> wrote:
stefan.duke at gmail.com:
I came across a strange behavior of expand.grid (or at least strange to me). For certain values of one of my input variables - created by seq() - I have to use strings (e.g ==".6") to select a row of the object created by expand.grid(), for other values numerical (e.g. ==.8) and for some both work.
It?s not a bug. Please see http://wiki.r-project.org/rwiki/doku.php?id=misc:r_accuracy&s=floating and the documents referenced therein. -- Karl Ove Hufthammer
______________________________________________ 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.