# 1. Libraries ------------------------------------------------------------

library(tidyr)
library(xlsx)
## Loading required package: rJava
## Loading required package: xlsxjars
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(base)


# 2. Import data ----------------------------------------------------------


XYZ <- read.xlsx("data_v5_3var_R.xlsx", 
                sheetIndex = 1)
head(XYZ)
##   ID ConditionA ConditionB ConditionC   X   Y    Z
## 1  1          2          1          3 7.0 0.4 10.8
## 2  2          1          1          1 8.0 5.4 12.0
## 3  3          2          2          2 2.4 7.6  4.8
## 4  4          1          2          2 3.0 6.2 12.0
## 5  5          1          1          3 4.2 0.2 12.0
## 6  6          2          1          2 2.8 3.8  8.4
names(XYZ)
## [1] "ID"         "ConditionA" "ConditionB" "ConditionC" "X"         
## [6] "Y"          "Z"
# 3. From wide to long ----------------------------------------------------



XYZ_long <- melt(XYZ, id.vars=c("ID", "ConditionA", "ConditionB", "ConditionC"))
head(XYZ_long)
##   ID ConditionA ConditionB ConditionC variable value
## 1  1          2          1          3        X   7.0
## 2  2          1          1          1        X   8.0
## 3  3          2          2          2        X   2.4
## 4  4          1          2          2        X   3.0
## 5  5          1          1          3        X   4.2
## 6  6          2          1          2        X   2.8
#rename variables

names(XYZ_long)[names(XYZ_long) == "variable"] <- "Brand"
names(XYZ_long)[names(XYZ_long) == "value"] <- "Score"
head(XYZ_long)
##   ID ConditionA ConditionB ConditionC Brand Score
## 1  1          2          1          3     X   7.0
## 2  2          1          1          1     X   8.0
## 3  3          2          2          2     X   2.4
## 4  4          1          2          2     X   3.0
## 5  5          1          1          3     X   4.2
## 6  6          2          1          2     X   2.8
nrow(XYZ_long)
## [1] 297
write.csv(XYZ_long, "XYZ_long.csv")

# 5. From long to wide ----------------------------------------------------


XYZ_wide <- dcast(XYZ_long, ID + ConditionA + ConditionB + ConditionC ~
                      Brand, value.var="Score", fun.aggregate = mean)
XYZ_wide
##    ID ConditionA ConditionB ConditionC   X   Y    Z
## 1   1          2          1          3 7.0 0.4 10.8
## 2   2          1          1          1 8.0 5.4 12.0
## 3   3          2          2          2 2.4 7.6  4.8
## 4   4          1          2          2 3.0 6.2 12.0
## 5   5          1          1          3 4.2 0.2 12.0
## 6   6          2          1          2 2.8 3.8  8.4
## 7   7          1          1          2 9.2 0.2 10.8
## 8   8          2          1          3 2.0 6.8 12.0
## 9   9          2          1          2 5.6 0.6  9.6
## 10 10          2          2          2 9.6 0.2 10.8
## 11 11          1          1          3 3.2 0.4  4.8
## 12 12          1          1          3 6.0 0.6  4.8
## 13 13          1          1          3 6.4 0.4  6.0
## 14 14          1          1          2 9.6 0.2  6.0
## 15 15          2          1          1 6.2 0.4  6.0
## 16 16          2          1          1 4.4 0.2  7.2
## 17 17          2          2          3 2.4 0.6 12.0
## 18 18          2          1          2 7.8 5.8  1.2
## 19 19          1          1          1 3.8 7.4  3.6
## 20 20          1          2          2 5.0 0.4  8.4
## 21 21          2          1          1 2.4 6.0  6.0
## 22 22          2          2          1 2.4 2.4  6.0
## 23 23          1          2          3 8.6 6.0  2.4
## 24 24          2          1          2 8.4 6.0  7.2
## 25 25          2          1          1 8.6 9.8  9.6
## 26 26          1          2          2 9.8 8.6  2.4
## 27 27          1          1          2 2.4 4.8  8.4
## 28 28          2          1          1 3.6 8.4  6.0
## 29 29          1          2          2 6.0 9.6 10.8
## 30 30          1          2          1 9.6 9.6  1.2
## 31 31          1          2          1 9.6 7.2  9.6
## 32 32          2          2          3 8.4 8.6  1.2
## 33 33          2          1          1 7.2 7.2  3.6
## 34 34          2          1          1 9.8 7.2  2.4
## 35 35          1          1          2 8.6 2.4 12.0
## 36 36          1          1          2 8.4 3.6  4.8
## 37 37          2          1          2 6.0 4.8  4.8
## 38 38          2          1          1 4.8 2.4  4.8
## 39 39          1          1          1 9.8 9.8  2.4
## 40 40          1          2          2 8.6 7.2  7.2
## 41 41          2          2          1 9.6 7.2  6.0
## 42 42          1          2          1 6.0 3.6 12.0
## 43 43          1          1          2 3.6 9.6  9.6
## 44 44          2          2          3 3.6 9.8 10.8
## 45 45          2          2          1 4.8 9.6  3.6
## 46 46          2          2          2 2.4 9.8  2.4
## 47 47          2          2          3 9.6 6.0  9.6
## 48 48          2          1          3 4.8 8.6  8.4
## 49 49          2          1          3 6.0 2.4  9.6
## 50 50          1          2          3 3.6 6.0  3.6
## 51 51          1          2          2 8.6 8.4  3.6
## 52 52          1          1          1 8.6 9.8  8.4
## 53 53          2          1          2 1.2 3.6  1.2
## 54 54          1          2          3 7.2 4.8  2.4
## 55 55          1          2          2 9.8 9.6  2.4
## 56 56          2          1          1 7.2 6.0  2.4
## 57 57          2          2          3 1.2 6.0  2.4
## 58 58          2          2          1 7.2 3.6  7.2
## 59 59          1          2          3 6.0 8.6  8.4
## 60 60          1          1          1 9.8 9.6  2.4
## 61 61          2          1          2 3.6 2.4  4.8
## 62 62          2          2          3 8.4 9.6 12.0
## 63 63          1          1          1 6.0 4.8  7.2
## 64 64          1          2          3 4.8 9.8 10.8
## 65 65          2          2          3 1.2 9.6  9.6
## 66 66          2          1          1 3.6 4.8  4.8
## 67 67          2          1          2 8.6 6.0  9.6
## 68 68          2          2          1 8.6 8.6  9.6
## 69 69          1          1          3 2.4 8.4  9.6
## 70 70          1          1          2 2.4 4.8  4.8
## 71 71          2          2          2 9.8 8.6 10.8
## 72 72          1          1          2 9.8 3.6  6.0
## 73 73          2          1          3 2.4 4.8  8.4
## 74 74          1          1          1 9.6 7.2  1.2
## 75 75          2          1          2 1.2 4.8  6.0
## 76 76          1          2          1 6.0 3.6  3.6
## 77 77          1          1          1 8.6 1.2 12.0
## 78 78          1          1          1 8.4 9.6  4.8
## 79 79          1          1          2 4.8 9.6  2.4
## 80 80          1          1          1 4.8 8.6  1.2
## 81 81          2          1          1 6.0 9.8  3.6
## 82 82          1          2          3 9.8 6.0 10.8
## 83 83          2          1          3 2.4 9.6  3.6
## 84 84          2          2          1 1.2 8.6  4.8
## 85 85          2          1          1 3.6 3.6 10.8
## 86 86          1          1          3 3.6 4.8 10.8
## 87 87          1          2          1 8.4 7.2  6.0
## 88 88          2          2          2 7.2 1.2  4.8
## 89 89          1          2          2 3.6 3.6 10.8
## 90 90          1          1          2 1.2 9.8  6.0
## 91 91          1          2          1 7.2 9.8  7.2
## 92 92          1          1          2 2.4 1.2  7.2
## 93 93          1          1          3 8.4 9.6  1.2
## 94 94          1          2          1 6.0 9.6  8.4
## 95 95          2          1          1 1.2 8.4  1.2
## 96 96          1          2          1 7.2 1.2 12.0
## 97 97          1          1          1 4.8 4.8  3.6
## 98 98          1          2          3 1.2 4.8  4.8
## 99 99          1          1          2 7.2 7.2 10.8
nrow(XYZ_wide)
## [1] 99
write.csv(XYZ_wide, "XYZ_wide.csv")