[R] transform table to matrix

Ista Zahn izahn at psych.rochester.edu
Wed Mar 2 17:21:08 CET 2011


Hi Sk,

On Wed, Mar 2, 2011 at 8:43 AM, SK MAIDUL HAQUE <skmaidulhaque 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.

dat.m <- as.matrix(dat)
rownames(dat.m) <- dat[, 1]
dat.m <- dat.m[, -1]

However, since the
> read.tablefunction imported the data as an indexed data frame, I can't use
> the columns
> as vectors.

I'm not sure why you can't access the collumns in a data.frame. Of
course you can -- see ?"[" or ?subset

Is there a way around this, to convert the data frame as 3
> separate vectors?

?"["

Best,
Ista

I have been looking all over for a function, and my
> programming skills are not great.
>
>
> --
> Sk Maidul Haque
> Scientific Officer-C
> Applied Spectroscopy Division
> Bhabha Atomic Research Centre, Vizag
>
> Mo: 09666429050/09093458503
>
>        [[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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list