Subject: Re: [R] Turn three Columns into a Matrix? On Feb 23, 2010, at 3:18 PM, Ortiz, John wrote:
> Hi all, > > If I have a data frame with 3 columns as follows: > >> ta > > Species Depth Counts > spc_a 120 60 > spc_a 140 140 > spc_b 140 5 > spc_b 150 4 > spc_b 180 10 > spc_c 180 10 > spc_c 190 20 > > How can I turn it into a dataframe or matrix with this structure?: > > > 120 140 140 150 180 180 190 > spc_a 60 0 0 0 0 0 0 > spc_a 0 140 0 0 0 0 0 > spc_b 0 0 5 0 0 0 0 > spc_b 0 0 0 4 0 0 0 > spc_b 0 0 0 0 10 0 0 > spc_c 0 0 0 0 0 10 0 > spc_c 0 0 0 0 0 0 20 > > I tried with matrify, but this function summarized. > > library(labdsv) > matrify(ta) > > 120 140 150 180 190 > spc_a 60 140 0 0 0 > spc_b 0 5 4 10 0 > spc_c 0 0 0 10 20 > > We are looking by one function similarly to matrify but without > summary. Not sure what that last sentence means but here is a a solution to above request: > ta <- read.table(textConnection(" + + Species Depth Counts + spc_a 120 60 + spc_a 140 140 + spc_b 140 5 + spc_b 150 4 + spc_b 180 10 + spc_c 180 10 + spc_c 190 20"), header=T) > tdiag <- diag(ta$Counts, nrow=nrow(ta), ncol=nrow(ta)) > rownames(tdiag)<-ta$Species > colnames(tdiag)<-ta$Depth > tdiag 120 140 140 150 180 180 190 spc_a 60 0 0 0 0 0 0 spc_a 0 140 0 0 0 0 0 spc_b 0 0 5 0 0 0 0 spc_b 0 0 0 4 0 0 0 spc_b 0 0 0 0 10 0 0 spc_c 0 0 0 0 0 10 0 spc_c 0 0 0 0 0 0 20 Yes this is what I was looking for. Thanks But this solution doesn't work in my case, because I have 270.000 Rows I tried with 10.000 Rows and work good, but 30.000 give me this error: Error: cannot allocate vector of size 3.4 Gb And with 270.000 rows this error: Error in array(0, c(n, p)) : 'dim' specifies too large an array Somebody Know other solution? > > some advice? > > Thanks!! > > John Ortiz > Smithsonian Tropical Research Institute > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.