0

I need some help using r.

In fact I have a data frame such as :

Groups  Species Value
Group1  Sp1 0.1
Group1  Sp1 0.2
Group1  Sp2 0.3
Group1  Sp3 0.4
Group1  Sp4 0.5
Group2  Sp2 0.6
Group2  Sp3 0.7
Group2  Sp4 0.8
Group3  Sp1 0.9
Group3  Sp3 0.10
Group3  Sp3 0.11
Group3  Sp3 0.14

(By the way if someone has a function that allows to export the data frame in coding format it would be better I guess)

And the idea is to transform this data frame to another one.

As you can see there are 3 groups, and within groups we can have duplicate species such as the Sp1 in Group1.

So I should get a matrix such as :

    Group1  Group2  Group3
Sp1 0.15    NA  0.9
Sp2 0.3 0.6 NA
Sp3 0.4 0.7 0.1167
Sp4 0.5 0.8 NA

As you can see from this output I put Groups in column et Species in rows and the values are the Values in the first data frame.

But the suptelity is that I calculate the mean of these values when there is duplicates such as Sp1 in Group1 or Sp3 in Group3.

Does anyone have an idea in order to do that? The ultime objectif is to get a heatmap from this matrix.

Thank you for your help.

chippycentra
  • 879
  • 1
  • 6
  • 15

1 Answers1

1

summarise take mean and spread

library(dplyr)

df %>%
 group_by(Groups, Species) %>%
 summarise(Value = mean(Value)) %>%
 tidyr::spread(Groups, Value)

#  Species Group1 Group2 Group3
#  <fct>    <dbl>  <dbl>  <dbl>
#1  Sp1       0.15    NA   0.9  
#2  Sp2       0.3     0.6  NA    
#3  Sp3       0.4     0.7  0.117
#4  Sp4       0.5     0.8  NA    
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213