Reshaping an array to data.frame
df = melt(x)
gives you something very similar to what you want. Then you could compute the various measure variables from the different levels of measure.
Using the "reshape2" package, try:
dcast(melt(x), Subject + Cond + Item ~ Var4)
Yes, use adply()
:
adply(x, c(1,2,3))
Subject Cond Item Measure1 Measure2 Measure3
1 s1 A 1 -0.93 -0.360 -0.005
2 s2 A 1 0.39 1.043 1.090
3 s3 A 1 0.88 0.330 0.360
4 s4 A 1 0.63 -0.120 0.040
5 s5 A 1 0.86 -0.055 0.090
6 s1 B 1 -0.69 0.070 0.170
7 s2 B 1 1.02 0.670 0.680
8 s3 B 1 0.29 0.480 0.510
9 s4 B 1 0.94 0.002 0.090
10 s5 B 1 0.93 0.008 0.120
11 s1 A 2 -0.01 -0.190 -0.050
12 s2 A 2 0.79 -1.390 0.110
13 s3 A 2 0.32 0.980 0.990
14 s4 A 2 0.14 0.430 0.620
15 s5 A 2 0.13 -0.020 0.130
16 s1 B 2 -0.07 -0.150 0.060
17 s2 B 2 -0.63 -0.080 0.270
18 s3 B 2 0.26 0.740 0.740
19 s4 B 2 0.07 0.960 0.960
20 s5 B 2 0.87 0.440 0.450
Use as.data.frame.table()
.
d0 <- as.data.frame.table(x)
head(d0)
# Subject Cond Item Var4 Freq
# 1 s1 A 1 Measure1 -0.93
# 2 s2 A 1 Measure1 0.39
# 3 s3 A 1 Measure1 0.88
# 4 s4 A 1 Measure1 0.63
# 5 s5 A 1 Measure1 0.86
# 6 s1 B 1 Measure1 -0.69
library(tidyr)
d1 <- pivot_wider(data = d0, names_from = "Var4", values_from = "Freq")
head(d1)
# Subject Cond Item Measure1 Measure2 Measure3
# 1 s1 A 1 -0.93 -0.360 -0.005
# 2 s1 A 2 -0.01 -0.190 -0.050
# 3 s1 B 1 -0.69 0.070 0.170
# 4 s1 B 2 -0.07 -0.150 0.060
# 5 s2 A 1 0.39 1.043 1.090
# 6 s2 A 2 0.79 -1.390 0.110