0

I have a dataframe with multiple groups that appear in various samples and would like to organise it by samples

I have several groups of variables with different values. Each group is part of a sample and might appear in several samples (just different value for different sample) but might not. I wish to organise the data frame so every row is a sample, every column is a group and for samples for which there is no value, a 0 should be added.

Groups<-sample(1:9,replace = T)
SampData<-data.frame(Group=LETTERS[Groups],Value=sample(1:9,replace = T),Sample=c(1,2,3,1,1,2,2,4,3))

This is a sample data frame:

   Group Value Sample
1     A     5      1
2     I     5      2
3     A     1      3
4     H     7      1
5     E     7      1
6     D     6      2
7     B     1      2
8     E     4      4
9     B     6      3  

I would like to reorganise it so it looks like this:

   A  B  D  E  H  I
1  5  0  0  7  7  0
2  0  1  6  0  0  5
3  1  6  0  0  0  0
4  0  0  0  4  0  0  
jogo
  • 12,469
  • 11
  • 37
  • 42
B.Shermeister
  • 507
  • 2
  • 6
  • 17

1 Answers1

0

You can use dcast within reshape2:

library(dplyr)
library(reshape2)
set.seed(123)

Groups<-sample(1:9,replace = T)
SampData<-data.frame(Group=LETTERS[Groups],Value=sample(1:9,replace = T),Sample=c(1,2,3,1,1,2,2,4,3))

df <- SampData %>%
  dcast(Sample ~ Group, value.var = "Value", fun.aggregate = sum)
Bjørn Kallerud
  • 979
  • 8
  • 23