[R] Transforming matrix to data.frame: problems

Agustin Lobo alobo at ija.csic.es
Mon Nov 12 10:53:57 CET 2001


Thanks for the answer, 

here is the output of str()

> str(parcelas.cobtot.conti)
 xtabs [1:25, 1:64] 0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, "dimnames")=List of 2
  ..$ v1: chr [1:25] "6" "13" "15" "23" ...
  ..$ v2: chr [1:64] "11" "19" "25" "28" ...
 - attr(*, "class")= chr [1:2] "xtabs" "table"
 - attr(*, "call")= language xtabs(formula = z ~ v1 + v2)

Also, the first 2 lines and 10 cols. of parcelas.cobtot.conti:

> parcelas.cobtot.conti[1:2,1:10]
    v2
v1   11   19   25 28 38 39 42  44    46 48
  6   0 1.25 1.25  0  0  0  0 2.5  1.25  0
  13  0 0.00 0.00  0  0  0  0 0.0 24.25  0

while,

>  a  <- data.frame(parcelas.cobtot.conti)
> a[1:2,]
  v1 v2 Freq
1  6 11    0
2 13 11    0

and 
>  a  <- as.data.frame(parcelas.cobtot.conti)
> a[1:2,]
  v1 v2 Freq
1  6 11    0
2 13 11    0

Note:
parcelas.cobtot.conti was produced by 
xtabs(z~v1+v2)

where v1 are sites (6,13,...), v2 are species (11   19   25 28 38 ...)
and z a vector of area covered by each sps in each site:

> summary(parcelas.cobtot.conti)
Call: xtabs(formula = z ~ v1 + v2)
Number of cases in table: 3907.25 
Number of factors: 2 
Test for independence of all factors:
        Chisq = 16235, df = 1512, p-value = 0
        Chi-squared approximation may be incorrect

According to the doc, the output 
of as.data.frame(parcelas.cobtot.conti)
is the one of as.data.frame.table(parcelas.cobtot.conti)
and, therefore, object "a" is as described by the doc.
Now, then, the question becomes 
how can I get a data.frame with the same dimensions of
parcelas.cobtot.conti ?

My goal is runing MASS function corresp(parcelas.cobtot.conti), and I find
that corresp only accepts data.frames (which is in contradiction
with the doc, but this is a different issue that I send in another
message).

Agus


On 9 Nov 2001, Peter Dalgaard BSA wrote:

> Agustin Lobo <alobo at ija.csic.es> writes:
> 
> > Hi all,
> > 
> > I have problems transforming a matrix 
> > into a data.frame:
> > 
> > If I do:
> > 
> > > is.matrix(parcelas.cobtot.conti)
> > [1] TRUE
> > > dim(parcelas.cobtot.conti)
> > [1] 25 64
> > 
> > > a  <- data.frame(parcelas.cobtot.conti)
> > > is.data.frame(a)
> > [1] TRUE
> > > 
> > > dim(a)
> > [1] 1600    3
> > 
> > Why a does not have the same dimensions than 
> > parcelas.cobtot.conti?
> ...
> > What can be wrong with parcelas.cobtot.conti ?
> > I'd appreciate any help
> 
> Anything interesting coming out of 
> 
> str(parcelas.cobtot.conti)
> 
> ?
> 
> 
> -- 
>    O__  ---- Peter Dalgaard             Blegdamsvej 3  
>   c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
>  (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
> 


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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