1

I have data like this:

       sp    rd  pH abund area point         dd
1     dog  uniq 4.5     5    1     a   dog.uniq
2  rabbit   lol 4.5     5    1     a rabbit.lol
3     dog   for 4.5     3    1     a    dog.for
4     cat  uniq 4.5     8    1     a   cat.uniq
5     cat mains 4.5     5    1     a  cat.mains
6     cat mains 3.1     9    1     b  cat.mains
7     dog   for 3.1     1    1     b    dog.for
8     dog  uniq 3.1     3    1     b   dog.uniq
9     dog   for 5.1     2    2     a    dog.for
10   bird   mac 5.1     3    2     a   bird.mac
11    dog  uniq 5.1     5    2     a   dog.uniq
12 rabbit   lol 5.1     7    2     a rabbit.lol
13    dog  uniq 4.2     8    2     b   dog.uniq
14    cat mains 4.2     5    2     b  cat.mains
15 rabbit   lol 4.2     2    2     b rabbit.lol
16    dog   for 4.2     3    2     b    dog.for

and I want to transpone them to take information about abund depend on dd. So my expected data should look like:

  area point  pH dog.uniq dog.for cat.uniq cat.mains bird.mac rabbit.lol
1    1     a 4.5        5       3        8         5       NA          5
2    1     b 3.1        3       1       NA         9       NA         NA
3    2     a 5.1        5       2       NA        NA        3          7
4    2     b 4.2        8       3       NA         5       NA          2

I have already tried

datb <- acast(data, (abund+point+area)~dd)

or some modifications, but i obviously do not fully understand how this function works.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Bobesh
  • 1,157
  • 2
  • 15
  • 30

1 Answers1

2

We can use dcast

 library(reshape2)
 dcast(df1, area+point+pH ~dd, value.var='abund')
akrun
  • 874,273
  • 37
  • 540
  • 662