Data are in a messy format in which it is very difficult to work properly.
There are different ways to rearrange the data in a better format, here I propose the more recent way to do it with tidyr
and dplyr
:
library(dplyr)
library(tidyr)
tennis.nice <- tennis %>%
gather(ytype_time, pain, -c(id, age, sex, order)) %>%
separate(ytype_time, c('ytype', 'period'), sep=-2)
tennis.nice$Treatment[tennis.nice$order==1 & tennis.nice$period==1]="Motrin"
tennis.nice$Treatment[tennis.nice$period==2]="Washout"
tennis.nice$Treatment[tennis.nice$order==1 & tennis.nice$period==3]="Placebo"
tennis.nice$Treatment[tennis.nice$order==2 & tennis.nice$period==3]="Motrin"
tennis.nice$Treatment[tennis.nice$order==2 & tennis.nice$period==1]="Placebo"
tennis.nice[,c(1,3,4,5,6,8)] <- lapply(tennis.nice[,c(1,3,4,5,6,8)], as.factor)