How to create a binary matrix from a data.frame file with two columns, where the first column represents, for example. species, and their other region? The data.frame format is presented in high format, as shown 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 will have all unique views in the form of rows and all unique regions in the form of columns. The matrix will be filled in 1sfor present species and 0sfor absent species, as shown 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 greatly appreciated, thanks!
source
share