[R] matrix creation -- answer: data.matrix()
Martin Maechler
maechler at stat.math.ethz.ch
Tue Jan 29 10:38:23 CET 2008
>>>>> "GS" == Gavin Simpson <gavin.simpson at ucl.ac.uk>
>>>>> on Tue, 29 Jan 2008 00:09:05 +0000 writes:
GS> hits=-2.6 tests=BAYES_00
GS> X-USF-Spam-Flag: NO
GS> 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...
GS> That looks fine to me Michelle.
GS> You will have problems with as.matrix on this though as a matrix in R
GS> has to contain *all* elements of the same kind, text or numeric. As $X
GS> is a factor, the matrix ends up as a character matrix, as this example
GS> shows:
GS> my.dat <- data.frame(X = gl(4,5), Vegetative = runif(20), Dormant =
GS> runif(20))
GS> str(my.dat)
GS> as.matrix(my.dat)
[...............]
GS> You could convert the factor column to its numeric representation and
GS> then you can produce a matrix that is numeric
GS> my.dat$X <- as.numeric(my.dat$X)
GS> str(my.dat)
GS> as.matrix(my.dat)
That's exactly what the --- seemlingy much underused ---
function
data.matrix()
is for !
my.dat <- data.frame(X = gl(4,5), Veg = runif(20), Dorm = runif(20))
my.mat <- data.matrix(my.dat)
str(my.mat) # all numeric
In short:
Use data.matrix(d), not as.matrix(d)
if d potentially is a data frame !!
Regards,
Martin
GS> *But* you should read the help for popbio to see what you are required
GS> to provide as what I show is only a workaround for the matrix issue - I
GS> have no idea what a projection matrix is in the sense of popbio or how
GS> to use the functions in that package.
GS> HTH
GS> 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
More information about the R-help
mailing list