[R] Turn three Columns into a Matrix?

Ortiz, John OrtizJ at si.edu
Tue Feb 23 22:18:56 CET 2010


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.

some advice?

Thanks!!

John Ortiz
Smithsonian Tropical Research Institute


More information about the R-help mailing list