Using my Data
, I was wondering if there is a way to achieve my Desired_output
(an object of class table
or matrix
)?
I tried the following without success:
with(Data, table(group, task_type, time.))
Data = data.frame(group=rep(c("Sim","Com"), each=4),
task_type = c(rep(c("S","C"),2),rep(c("C","S"),2)),
time = time <- rep(1:4,2),
time. = ifelse(time%%2==1, "odd", "even"))
Desired_output="
task_type
group C S
Com odd even
Sim even odd
"
We may get the distinct
rows before reshaping to 'wide' with pivot_wider
and converting to table
library(dplyr)
library(tidyr)
out <- Data %>%
distinct(group, task_type, time.) %>%
pivot_wider(names_from = task_type, values_from = time.) %>%
column_to_rownames("group") %>%
as.matrix %>%
as.table
names(dimnames(out)) <- names(Data)[1:2]
-output
> out
task_type
group S C
Sim odd even
Com even odd