# How to arrange my data for Friedman's test

I have conducted a study where participants have seen photos of people in different categories, let's call them A, B C, and D, and have ranked four pictures in each category on 1st choice to 4th choice. So essentially in each category there will be four rankings between 1-4 per participant. I want to conduct a Friedman test to see which category is ranked first, second, third and fourth. So far I've calculated the mean ranking per each category per each participant, thus ended up with a 78x4 matrix. I've run the test but I'm now doubting if I should have ran it on the mean rankings. There is technically no ranking of for example, 1.8 so I'm wondering if my results are going to be misleading?

A reproducible data sample


library(tidyverse)

original.dat <- data.frame(sbj = rep(1:4, each =  16),
category = rep(c("A", "B", "C", "D"), 16),
rank = sample(1:4, 64, replace = T))

# Summarised dataset used for analysis

dat <- original.dat %>%
group_by(sbj, category) %>%
summarise(m_rank = mean(rank)) %>%
ungroup()



Any help would be appreciated!

I think you have a small error when building the table used to the Friedman's test.

Since you have 4 replicates, you may need to consider each of them individually (and not summarize the average result, as you're doing).

Thus, I recommend you to do the following:

original.dat <- data.frame(sbj = rep(1:4, each =  16),
replicate = rep(1:4, each = 4),
category = rep(c("A", "B", "C", "D"), 16),
rank = sample(1:4, 64, replace = T))

dat <- original.dat %>%
group_by(sbj, category,replicate) %>%
ungroup()


Note that I added a replicate column, which was used by dplyr.

Then, you can perform the Fridman's test using as input the four columns of this data.frame (A, B, C, and D). You will get the following data.frame:

However, the standard R implementation of Friedman's test only accepts a matrix as input. Thus, you may convert your data.frame to a matrix and run the Fridman's test as follows.

friedman.test(as.matrix(dat[,3:6]))


Hope you get it!

• Right, it's so each subject has their own small 'matrix of 4x4'? Commented Jul 15, 2019 at 19:23
• Yeah, you're right. I modified my answer to include a screenshot of the resulting data.frame. Commented Jul 15, 2019 at 19:28
• I ran it and saw it myself, thank you! It's actually close to what the original data looks like. Commented Jul 15, 2019 at 19:40