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]]
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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
I was asked for the following information and hope it might help those who could answer my question... To import the table I used: AsMi05test=read.csv("C:/AsMi_Site05_1998.csv")> str(AsMi05test)`data.frame': 12 obs. of 8 variables: $ X : Factor w/ 6 levels "Dead","Dormant",..: 5 6 3 4 2 1 5 6 3 4 ... $ Vegetative : num 0.25 0.50 0.17 0.08 0.00 ... $ Vegetative.with.Herbivory : num 0.13 0.5 0.33 0.67 0 0 0.41 0.5 0 0 ... $ Reproductive : num 0 0 0 0 0 0 0 0 0 0 ... $ Reproductive.with.Herbivory: num 0 0 0 0 0 ... $ Dormant : num 0.08 0 0.33 0.08 1 0 0.06 0 0 0 ... $ Dead : num 0.42 0.00 0.33 0.17 0.00 ... $ End.Date : int 1998 1998 1998 1998 1998 1998 1999 1999 1999 1999 ... ________________________________ From: Michelle DePrenger-Levin Sent: Monday, January 28, 2008 11:35 AM To: 'r-help@r-project.org' Subject: matrix creation 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]]