8

How can I create a binary matrix from a data.frame with two columns where the first column represents e.g. species and the other their region? The data.frame is in tall format as seen below

species region
species1 1
species1 2
species1 3
species2 2
species2 4
species2 5
species2 6
species3 1
species3 2
species4 5
species5 3
species5 4

And the matrix would have all unique species as rows and all unique regions as columns. The matrix would be filled with 1s for species present and 0s for species absent, as below

         1  2  3  4  5  6
species1 1  1  1  0  0  0
species2 0  1  0  1  1  1
species3 1  1  0  0  0  0
species4 0  0  0  0  1  0
species5 0  0  1  1  0  0

Any pointers would be very much appreciated, thanks!

2 Answers 2

6

You are looking for the function table whose documentation is here

If your data.frame is df, you just have to do

table(df)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for commenting. Dooh' embarrassingly easy.
I'm getting an error: attempt to make a table with >= 2^31 elements. Is there a way to do it with bigger dataframes?
3

an other possibility :

xtabs(~species+region, data=tab)

2 Comments

Thanks for commenting and for an alternative solution.
xtabs fonction as table is dedicated to create contingency tables, you just have to indicate which lines and columns you want to cross

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.