[R] Deos anyone know of a summary of conformability rules? along with a specific problem

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat Jul 4 10:54:55 CEST 2009


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




More information about the R-help mailing list