[R] matrix creation

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Jan 28 19:50:36 CET 2008


hits=-2.6 tests=BAYES_00
X-USF-Spam-Flag: NO

Hi Michelle,

You don't show your read.csv or read.table call, nor the output of
str(obj) where obj is the name of the object you read the data into.

I notice that you have explicit 0 and NA. Is there a chance that you
have entered NA into the cells that you want to be missing in Excel? (I
know this may be a silly question but a user I know did this once.) If
you have, delete the NA's from the spreadsheet and leave those cells
blank and try again - R knows what to do in those cases and codes the
missingness as NA.

Also, remember that you can have text only in row 1 and/or column 1 for
the rownames and colnames.

Check out the R Data Import/Export that came with your R, which is also
on the web: http://cran.r-project.org/doc/manuals/R-data.html

HTH

G

On Mon, 2008-01-28 at 11:35 -0700, Michelle DePrenger-Levin wrote:
> Hello,
> 
>  
> 
> I am trying to create multiple matrices (to run a PVA) but can't import all
> of them from a .csv without the numbers treated as labels and not factors. 
> 
>  
> 
> I can enter the matrix slowly:
> 
> Site05_96 <- matrix(c(0.07,0,0.03,0.00,NA,0.00,
> 0.09,0.166666667,0.31,0.42,NA,0.00,  0.00,0,0.00,0.00,NA,0.00, 
> 
>                         0.00,0,0.00,0.00,NA,0.00,
> 0.26,0.166666667,0.19,0.00,NA,0.00,  0.58,0.666666667,0.47,0.58,0,0.00),
> 
>             nrow = 6, ncol = 6, 
> 
>             dimnames = list(c("Vegetative", "Vegetative with herbivory",
> "Reproductive", 
> 
>                         "Reproductive with herbivory", "Dormant", "Dead"),
> c("Vegetative", "Vegetative with herbivory", "Reproductive", 
> 
>                         "Reproductive with herbivory", "Dormant", "Dead")))
> 
>  
> 
> I would like to list all matrices (for all 12 years and all 4 sites) in one
> Excel sheet (.csv) and then read each matrix as chucks of 6 rows. However,
> when I try this I either get all the values (the %) in quotes (not as
> factors) and if I try to force them with as.factor, it no longer seems to be
> a matrix. 
> 
>  
> 
> AsMi0598test2 <- as.matrix(AsMi05test[1:6,1:6])
> 
>   X                             Vegetative Vegetative.with.Herbivory
> Reproductive Reproductive.with.Herbivory Dormant
> 
> 1 "Vegetative"                  "0.25"     "0.13"                    "0"
> "0"                         "0.08" 
> 
> 2 "Vegetative with Herbivory"   "0.50"     "0.50"                    "0"
> "0"                         "0.00" 
> 
> 3 "Reproductive"                "0.17"     "0.33"                    "0"
> "0"                         "0.33" 
> 
> 4 "Reproductive with Herbivory" "0.08"     "0.67"                    "0"
> "0"                         "0.08" 
> 
> 5 "Dormant"                     "0.00"     "0.00"                    "0"
> "0"                         "1.00" 
> 
> 6 "Dead"                        "0.00"     "0.00"                    "0"
> "0"                         "0.00"
> 
>  
> 
> When I add AsMi0598test2 <- as.factor(as.matrix(AsMi05test[1:6,1:6])) I get
> this:
> 
> [1] Vegetative                  Vegetative with Herbivory   Reproductive
> Reproductive with Herbivory
> 
>  [5] Dormant                     Dead                        0.25
> 0.50                       
> 
>  [9] 0.17                        0.08                        0.00
> 0.00                       
> 
> [13] 0.13                        0.50                        0.33
> 0.67                       
> 
> [17] 0.00                        0.00                        0
> 0                          
> 
> [21] 0                           0                           0
> 0                          
> 
> [25] 0                           0                           0
> 0                          
> 
> [29] 0                           0                           0.08
> 0.00                       
> 
> [33] 0.33                        0.08                        1.00
> 0.00                       
> 
> 16 Levels: 0 0.00 0.08 0.13 0.17 0.25 0.33 0.50 0.67 1.00 Dead Dormant
> Reproductive ... Vegetative with Herbivory
> 
>  
> 
> I want to read all matrices into stoch.projection( ) {popbio}
> 
>  
> 
> Thanks for any suggestions. I could enter them all in the .txt document if I
> can't read them from the Excel sheet. 
> 
>  
> 
> Michelle DePrenger-Levin
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list