?as.matrix On Friday 30 June 2006 09:11, Wade Wall wrote:> I have a text file that I have imported into R. It contains 3 columns > and 316940 rows. The first column is vegetation plot ID, the second > species names and the third is a cover value (numeric). I imported using > the read.table function. > > My problem is this. I need to reformat the information as a matrix, with > the first column becoming the row labels and the second the column labels > and the cover values as the matrix cell data. However, since the > read.tablefunction imported the data as an indexed data frame, I can't > use the columns > as vectors. Is there a way around this, to convert the data frame as 3 > separate vectors? I have been looking all over for a function, and my > programming skills are not great. > > Thanks in advance > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-- Michael W. Sears, Ph.D. Associate Editor, Herpetologica Assistant Professor Center for Ecology Department of Zoology Southern Illinois University Carbondale, IL 62901 phone: 618-453-4137 web: http://equinox.unr.edu/homepage/msears http://www.ecology.siu.edu "Natural selection is a mechanism for generating an exceedingly high degree of improbability" Sir Ronald A. Fisher (1890-1962)
I have a text file that I have imported into R. It contains 3 columns and 316940 rows. The first column is vegetation plot ID, the second species names and the third is a cover value (numeric). I imported using the read.table function. My problem is this. I need to reformat the information as a matrix, with the first column becoming the row labels and the second the column labels and the cover values as the matrix cell data. However, since the read.tablefunction imported the data as an indexed data frame, I can't use the columns as vectors. Is there a way around this, to convert the data frame as 3 separate vectors? I have been looking all over for a function, and my programming skills are not great. Thanks in advance [[alternative HTML version deleted]]
On 6/30/2006 10:11 AM, Wade Wall wrote:> I have a text file that I have imported into R. It contains 3 columns and > 316940 rows. The first column is vegetation plot ID, the second species > names and the third is a cover value (numeric). I imported using the > read.table function. > > My problem is this. I need to reformat the information as a matrix, with > the first column becoming the row labels and the second the column labels > and the cover values as the matrix cell data. However, since the > read.tablefunction imported the data as an indexed data frame, I can't > use the columns > as vectors. Is there a way around this, to convert the data frame as 3 > separate vectors? I have been looking all over for a function, and my > programming skills are not great.Internally, dataframes are just lists with a class="dataframe" attribute. This means you can extract the columns as if they were just lists. So if your columns are named A, B, and C, and the dataframe is dataf, you get them as vectors using dataf$A, dataf$B, and dataf$C Duncan Murdoch
On Fri, Jun 30, 2006 at 10:11:15AM -0400, Wade Wall wrote:> I have a text file that I have imported into R. It contains 3 columns and > 316940 rows. The first column is vegetation plot ID, the second species > names and the third is a cover value (numeric). I imported using the > read.table function. > > My problem is this. I need to reformat the information as a matrix, with > the first column becoming the row labels and the second the column labels > and the cover values as the matrix cell data.I'm not 100% sure but I think you are looking for reshape(). cu Philipp -- Dr. Philipp Pagel Tel. +49-8161-71 2131 Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186 Technical University of Munich Science Center Weihenstephan 85350 Freising, Germany and Institute for Bioinformatics / MIPS Tel. +49-89-3187 3675 GSF - National Research Center Fax. +49-89-3187 3585 for Environment and Health Ingolst?dter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/staff/pagel
Hi Wade, On 6/30/06, Wade Wall <wade.wall at gmail.com> wrote:> I have a text file that I have imported into R. It contains 3 columns and > 316940 rows. The first column is vegetation plot ID, the second species > names and the third is a cover value (numeric). I imported using the > read.table function. > > My problem is this. I need to reformat the information as a matrix, with > the first column becoming the row labels and the second the column labels > and the cover values as the matrix cell data.Try crosstab(mydata$plotID, mydata$species, mydata$cover) from the ecodist package. Sarah -- Sarah Goslee
I think this will do what you want: dframe <- read.table("your.text.file", <other args as needed> ) mat <- tapply(dframe[,3],dframe[,1:2],c) On Fri, 30 Jun 2006, Wade Wall wrote:> I have a text file that I have imported into R. It contains 3 columns and > 316940 rows. The first column is vegetation plot ID, the second species > names and the third is a cover value (numeric). I imported using the > read.table function. > > My problem is this. I need to reformat the information as a matrix, with > the first column becoming the row labels and the second the column labels > and the cover values as the matrix cell data. However, since the > read.tablefunction imported the data as an indexed data frame, I can't > use the columns > as vectors. Is there a way around this, to convert the data frame as 3 > separate vectors? I have been looking all over for a function, and my > programming skills are not great. > > Thanks in advance > > [[alternative HTML version deleted]] > > > > [ Part 3.23: "Included Message" ] >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0717