[R] Way to convert data frame to matrix

Duncan Murdoch murdoch at stats.uwo.ca
Fri Jun 30 16:21:56 CEST 2006

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 

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

More information about the R-help mailing list