0

I searched similar questions but couldn't find an answer to what I am trying to achieve. I have a dataset that is set up like so:

ID    Trial   Treatment   Frequency  Value   
A       1      Control    8000       65.1   
A       1      Top        8000       62.8    
A       1      Bottom     8000       60.3  
A       1      Control    9000       63.1   
A       1      Top        9000       66.2    
A       1      Bottom     9000       69.8
A       2      Control    8000       67.6   
A       2      Top        8000       63.4    
A       2      Bottom     8000       71.9 
A       2      Control    9000       59.7  
A       2      Top        9000       63.3  
A       2      Bottom     9000       57.2 

Each ID (altogether there are 27) is subjected to three Treatment of playbacks of sounds at either 8000 or 9000 Hz (Frequency). The process is repeated twice, meaning there are multiple Trial.

I want to use pivot_wider on the Treatment column to end up with a table that looks like:

ID   Trial   Frequency   Control   Top   Bottom   
A    1       8000        65.1      62.8  60.3
A    2       8000        67.6      63.4  71.9  
A    1       9000        63.1      66.2  69.8
A    2       9000        59.7      63.3  57.2

Reproducible data:

df <- read.table(text="ID  Trial  Treatment   Frequency  Value   
A       1      Control    8000  65.1   
A       1      Top    8000  62.8    
A       1      Bottom   8000  60.3  
A       1      Control    9000  63.1   
A       1      Top    9000  66.2    
A       1      Bottom  9000  69.8 
A       2      Control  8000  67.6  
A       2      Top      8000  63.4  
A       2      Bottom   8000   71.9 
A       2      Control    9000  59.7  
A       2      Top      9000  63.3  
A       2      Bottom     9000  57.2", strin=F,h=T)

1 Answers1

0

We could create an index column with rowid (if there duplicate elements for ID, Treatment) and then do the reshaping to wide format with pivot_wider

library(dplyr)
library(tidyr)
library(data.table)
df %>% 
    mutate(rid = rowid(ID, Treatment))  %>%
    pivot_wider(names_from = Treatment, values_from = c(Value))
# A tibble: 4 x 6
#  ID    Trial Frequency Control   Top Bottom
#  <chr> <int>     <int>   <dbl> <dbl>  <dbl>
#1 A         1      8000    65.1  62.8   60.3
#2 A         1      9000    63.1  66.2   69.8
#3 A         2      8000    67.6  63.4   71.9
#4 A         2      9000    59.7  63.3   57.2

Or another option is to apply a function i.e. mean for the 'Value' column so that we get the average value for any duplicates

df %>%
    pivot_wider(names_from = Treatment, values_from = c(Value), values_fn = mean) 
akrun
  • 874,273
  • 37
  • 540
  • 662