0

For example, I have a data frame looks like this, the 'species' column (total 8 species) represent species name, the 'site' column (total 4 sites) represent which site each species appearance

Example input:

#  species site
#1    sp1    1
#2    sp2    1
#3    sp3    1
#4    sp4    2
#5    sp5    2
#6    sp1    2
#7    sp2    2
#8    sp6    3
#9    sp1    3
#10   sp2    3
#11   sp7    4
#12   sp8    4
#13   sp6    4

Desired output:

#site sp1 sp2 sp3 sp4 sp5 sp6 sp7 sp8
#1     1   1   1   0   0   0   0   0
#2     1   1   0   1   1   0   0   0
#3     1   1   0   0   0   1   0   0
#4     0   0   0   0   0   1   1   1

How can I transfer this data frame into a data matrix, where rownames are sites name (i.e., 1 2 3 4) and column names are species name? And I want the final output to be a data matrix.

Yuhao Zhao
  • 29
  • 5
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 03 '20 at 08:54
  • Generally, I think you are looking for reshaping your data from long to wide format, but agree, it will help to get an example of your data + if you coudl share some code of what you already tried to achieve your goal. – deschen Dec 03 '20 at 09:10
  • Now I know how to add example – Yuhao Zhao Dec 03 '20 at 11:07

1 Answers1

0

I think you can try this to get a matrix with site as row names, and binary indicators of presence/absence for each site and species:

t(as.data.frame.matrix(+(table(df) > 0)))

Output

  sp1 sp2 sp3 sp4 sp5 sp6 sp7 sp8
1   1   1   1   0   0   0   0   0
2   1   1   0   1   1   0   0   0
3   1   1   0   0   0   1   0   0
4   0   0   0   0   0   1   1   1
Ben
  • 28,684
  • 5
  • 23
  • 45