How can I use dplyr/tidyr
to covert a data frame like this:
df <- data.frame(obj=c(1,1,2,2,3,3,3,4,4,4),
S1=rep(c("a","b"),length.out=10),S1PR=rep(c(3,7),length.out=10),
S2=rep(c("c","d"),length.out=10),S2PR=rep(c(7,3),length.out=10),
Relsize=c(.4,.6,.4,.6,.2,.2,.6,.2,.2,.6))
obj S1 S1PR S2 S2PR Relsize
1 1 a 3 c 7 0.4
2 1 b 7 d 3 0.6
3 2 a 3 c 7 0.4
4 2 b 7 d 3 0.6
5 3 a 3 c 7 0.2
6 3 b 7 d 3 0.2
7 3 a 3 c 7 0.6
8 4 b 7 d 3 0.2
9 4 a 3 c 7 0.2
10 4 b 7 d 3 0.6
And turn it in to one like this:
obj a b c d
1 0.12 0.42 0.28 0.18
2 0.12 0.42 0.28 0.18
3 0.24 0.14 0.56 0.06
The values in the output data frame are based on PR/10*Relsize. EDIT: where there is more than one entry for the same species they should be summed I've been trying to do this with some combination of spread and gather but I'm not sure how to do it.