I've been trying to create multiway tables with results, based on factor data.
My data looks like:
Trial Room Mechanism Result
A 1 Straight 0,5
A 1 Bendy 0,2
A 2 Straight 0.7
A 2 Bendy 0.3
B 1 Straight 0.6
B 1 Bendy 0.2
B 2 Straight 0.6
B 2 Bendy 0.2
With in reality about 6 factors with multiple levels and one column of results.
The result i'm looking for is like this:
Room 1 Room 2
A B A B
Straight 0.5 0.6 0.7 0.6
Bendy 0.2 0.2 0.3 0.2
Is there a function or a package that does this?
All the search results have yielded code to make multiway frequency tables from factors like with data.table() or count(). This is not what I am looking for. Maybe I'm using the wrong key words or maybe there is much more information on that issue.
Doing this manipulation by hand is an option but not preferred. And besides I'm not the first person needing to do this so I know there is a way!
Here is a base R solution, where reshape()
is used to re-format your data frame
df <- df[with(df,order(Room,Trial)),]
dfout <- reshape(within(df,RT <- apply(df[1:2],1,paste0,collapse = ""))[-(1:2)],
idvar = "Mechanism",
timevar = "RT",
direction = "wide")
such that
> dfout
Mechanism Result.A1 Result.B1 Result.A2 Result.B2
1 Straight 0.5 0.6 0.7 0.6
2 Bendy 0.2 0.2 0.3 0.2
DATA
df <- structure(list(Trial = c("A", "A", "B", "B", "A", "A", "B", "B"
), Room = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Mechanism = c("Straight",
"Bendy", "Straight", "Bendy", "Straight", "Bendy", "Straight",
"Bendy"), Result = c(0.5, 0.2, 0.6, 0.2, 0.7, 0.3, 0.6, 0.2)), row.names = c(1L,
2L, 5L, 6L, 3L, 4L, 7L, 8L), class = "data.frame")