Preparing data for two way anova in R -


this question has answer here:

i have following format of data:

    m1   m2   m3 names 1 24.5 28.4 26.1     1 2 23.5 34.2 28.3     2 3 26.4 29.5 24.3     3 4 27.1 32.2 26.2     4 5 29.9 20.1 27.8     5 

how can prepare data format can feed aov in r?

i.e.

   values ind name  1    24.5  m1 1 2    23.5  m1 2 3    26.4  m1 3 ... 

for 1 way anova used stack command. how can 2 way anova, without having loop?

an alternative above answers nice, can use gather tidyr package. gather takes multiple columns , collapses them key-value pairs. need pass 2 variables it, 1 key , other value

x<- structure(list(m1 = c(24.5, 23.5, 26.4, 27.1, 29.9), m2 = c(28.4,      34.2, 29.5, 32.2, 20.1), m3 = c(26.1, 28.3, 24.3, 26.2, 27.8),          names = 1:5), .names = c("m1", "m2", "m3", "names"), class = "data.frame", row.names = c(na,      -5l))   library(tidyr) dat <- x %>% gather(variable, value)  > head(dat,10) #   variable value #1        m1  24.5 #2        m1  23.5 #3        m1  26.4 #4        m1  27.1 #5        m1  29.9 #6        m2  28.4 #7        m2  34.2 #8        m2  29.5 #9        m2  32.2 #10       m2  20.1 

Comments