[R] write.table error

Martin Maechler maechler at stat.math.ethz.ch
Mon Jul 8 11:05:52 CEST 2002


>>>>> "RossD" == Ross Darnell <r.darnell at shrs.uq.edu.au> writes:

    RossD> OK . Point taken. Here is the original set of commands
    >> > > write.table(all,file="agree.csv",row.names=F)
    >> > Error in "rownames<-"(*tmp*, value = row.names(x)) :
    >> >     length of dimnames[1] not equal to array extent
    >> > > is.data.frame(all)
    >> > [1] TRUE
    >> > > dim(all)
    >> > [1] 854  18
    >> > > length(row.names(all))
    >> > [1] 854

    <<.......>>>>

    >> y <- do.call("cbind",lapply(all,"is.na"))
    >> rownames(y) <- row.names(all)
    RossD> Error in "rownames<-"(*tmp*, value = row.names(all)) : 
    RossD> length of dimnames[1] not equal to array extent

    RossD> Let's have a look at "y"

    >> str(y)
    RossD> logi [1:1708, 1:18] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ...
    RossD> - attr(*, "dimnames")=List of 2
    RossD> ..$ : NULL
    RossD> ..$ : chr [1:18] "time" "obs" "state1" "state2" ...
    >> 

    RossD> There is twice as many rows in "y" as there should be. Compare first
    RossD> 854 with second.
 
    >> sum(y[1:854,]!=y[1:854,])
    RossD> [1] 0

    RossD> Why does this happen with this particular data frame! Try each variable

    >> names(all)
    RossD> [1] "time"      "obs"       "state1"    "state2"    "name"      "agree"    
    RossD> [7] "symmetric" "Name"      "AWIA1"     "AWIA2"     "AS1"       "AS2"      
    RossD> [13] "AWAA1"     "AWAA2"     "DA1"       "DA2"       "DR1"       "DR2"      
    >> ...
    >> length(all$symmetric)
    RossD> [1] 1708

    RossD> This is twice as long as other variables. Why did I think this was
    RossD> impossible?  Can variables in data.frames by of different length?


    >> yy <- data.frame(v1=1:10,v2=1:15)
    RossD> Error in data.frame(v1 = 1:10, v2 = 1:15) : 
    RossD> arguments imply differing number of rows: 10, 15

    RossD> Slightly confused

as you show here, the answer is "no",
variables in data.frames must have compatible `size'.  
In the case of *vector* variables, they should all have equal length.

*However*
1) data.frames can contain _matrix_ variables in addition to
   vector ones.  Now, it's not the length but nrow() of the
   matrix which must be equal to ``nrow(<data.frame>)''  
   and the length of a 2 column matrix is just what you had : 2 * nrow()
   So my guess is,  the "symmetric" variable came from a matrix one ?

2) The data.frame class requires the above length/nrow
   compatibility, but in the current "S3" class system, there
   will always be many ways to construct data.frames (or any other
   class) of improper structure {hint: ?structure}.
   Not so however, if you use  data.frame() , as.data.frame()
   and other documented ways to construct data frames.

I think we should try to find out --offline maybe-- how you came
to have your "all" data.frame the way you got it.

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list