Steven Rytina
2009-Jul-03 17:29 UTC
[R] Deos anyone know of a summary of conformability rules? along with a specific problem
The rules for conformability of objects required for various operators remain a mystery as do some related problems like the rules for recycling in creating arrays etc. Would someone be able to direct me to where such rules are stated? In a related vein, there are all manner of operations that are readily coded in Gauss or in MATA that fail, often mysteriously, in R. Are there some guidelines available? And here is a specific problem: I would like to carry out logical comparisons between a (vector?) of length EYE and each row of a JAY by EYE object, obtaining a conforming JAY by EYE result of comparisons such as Obj[Jay,Eye] == Vec[Eye] Any thoughts on how to effect this would be welcome. Among many failed efforts, as.matrix(7 element Vec) == { an 8 row, 7 column data.frame} yields a 8 by 7 result v1 vs Ob[1,1] v2 vs Obj[1,2] ... v2 vs Ob[2,1] . . et cetera . v7 vs Obj[7,1] v1 vs Obj[8,1] while as.matrix(7 element Vec) == t{ the 8 row, 7 column data.frame} yields a conformability error. Thanks for any help or comments. [[alternative HTML version deleted]]
Peter Dalgaard
2009-Jul-04 08:54 UTC
[R] Deos anyone know of a summary of conformability rules? along with a specific problem
Steven Rytina wrote:> The rules for conformability of objects required for various operators remain a mystery as do some related problems like the rules for recycling in creating arrays etc. Would someone be able to direct me to where such rules are stated? > > In a related vein, there are all manner of operations that are readily coded in Gauss or in MATA that fail, often mysteriously, in R. Are there some guidelines available? > > And here is a specific problem: > > I would like to carry out logical comparisons between a (vector?) of length EYE and each row of a JAY by EYE object, obtaining a conforming JAY by EYE result of comparisons such as Obj[Jay,Eye] == Vec[Eye] > > Any thoughts on how to effect this would be welcome. > > Among many failed efforts, > > as.matrix(7 element Vec) == { an 8 row, 7 column data.frame} > > yields a 8 by 7 result > > v1 vs Ob[1,1] v2 vs Obj[1,2] ... > v2 vs Ob[2,1] . > . et cetera > . > v7 vs Obj[7,1] > v1 vs Obj[8,1] > > while > as.matrix(7 element Vec) == t{ the 8 row, 7 column data.frame} yields a conformability error. > > Thanks for any help or comments.Not sure what the actual rule set (much less the rationale) is, but the thing that seems to be happening is that (a) comparisons and other operations between matrices require strict conformance of dimensions. I.e. you cannot compare a 7x1 matrix to a 7x8 one. (b) comparisons of data frames to vectors removes any dimension attribute from the latter. So you can happily do as.matrix(1:8,2,4)==as.data.frame(matrix(1:8,8,7) but neither as.matrix(1:7)==matrix(1:8,7,8) nor as.matrix(1:7)==matrix(1:8,8,7) Transposing a data frame converts it to a matrix, so rule (a) kills you. What you can do is either drop the as.matrix() stuff so that you compare the transposed data frame to a vector, or (preferably) use a construct like sweep(D, V, 2, "==") -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
David Winsemius
2009-Jul-04 19:10 UTC
[R] Deos anyone know of a summary of conformability rules? along with a specific problem
On Jul 3, 2009, at 1:29 PM, Steven Rytina wrote:> The rules for conformability of objects required for various > operators remain a mystery as do some related problems like the > rules for recycling in creating arrays etc. Would someone be able to > direct me to where such rules are stated? > > In a related vein, there are all manner of operations that are > readily coded in Gauss or in MATA that fail, often mysteriously, in > R. Are there some guidelines available?Not sure. Haven't seen any. Generally the complaint posted here is that R is not Matlab. Here is one professor's answer to that one: http://germain.its.maine.edu/~hiebeler/comp/matlabR.pdf> > And here is a specific problem: > > I would like to carry out logical comparisons between a > (vector?) of length EYE and each row of a JAY by EYE object, > obtaining a conforming JAY by EYE result of comparisons such as > Obj[Jay,Eye] == Vec[Eye] > > Any thoughts on how to effect this would be welcome.> i =10; j=4 > vi <- 1:10 > mji <- matrix(1:40, nrow=j) > matrix( mapply("==", vi, mji[1:nrow(mji), ] ), nrow=nrow(mji) , byrow=TRUE) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE The fact that you have reversed the conventional order (for R anyway) of the indices makes me wonder it that is a source of confusion. You may have had problems in constructing an appropriate iterative row- wise comparison if you did not realize that the default order that matrix elements are filled and accessed is column-wise. The result of the mapply is just a long vector (and vi gets recycled) so it needs to be hammered back into conformance to get what you specified.> > Among many failed efforts, > > as.matrix(7 element Vec) == { an 8 row, 7 column data.frame} > > yields a 8 by 7 result > > v1 vs Ob[1,1] v2 vs Obj[1,2] ... > v2 vs Ob[2,1] . > . et cetera > . > v7 vs Obj[7,1] > v1 vs Obj[8,1] > > while > as.matrix(7 element Vec) == t{ the 8 row, 7 column data.frame} > yields a conformability error. > > Thanks for any help or comments. > [[alternative HTML version deleted]] > > ______________________________________________David Winsemius, MD Heritage Laboratories West Hartford, CT