Re: [R] matrix creation

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Tue, 29 Jan 2008 00:09:05 +0000

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

On Mon, 2008-01-28 at 12:17 -0700, Michelle DePrenger-Levin wrote:
> I was asked for the following information and hope it might help those who
> could answer my question...

That looks fine to me Michelle.

You will have problems with as.matrix on this though as a matrix in R has to contain *all* elements of the same kind, text or numeric. As $X is a factor, the matrix ends up as a character matrix, as this example shows:

my.dat <- data.frame(X = gl(4,5), Vegetative = runif(20), Dormant = runif(20))
str(my.dat)
as.matrix(my.dat)

I'm not familiar with popbio, but a brief look at the function you mentioned seems to need a list of matrices. You need to study what data requirements this function has. I doubt you can use the data you read in exactly as you have it, as you have found out you can't create a numeric matrix from it. This isn't an issue of how you are reading the data in, rather an inherent issue with how you are doing things with your data.

You could convert the factor column to its numeric representation and then you can produce a matrix that is numeric

my.dat$X <- as.numeric(my.dat$X)
str(my.dat)
as.matrix(my.dat)

*But* you should read the help for popbio to see what you are required to provide as what I show is only a workaround for the matrix issue - I have no idea what a projection matrix is in the sense of popbio or how to use the functions in that package.

HTH G

>
>
>
> 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_at_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]]
>
> ______________________________________________
> 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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
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.
Received on Tue 29 Jan 2008 - 00:12:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Jan 2008 - 10:30:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive