0

I have -simplified - a table in R with the following structure:

id coder evaluation
1  1     1
2  1     2
3  1     1
1  2     1
2  2     2
3  2     3
4  2     3
2  3     3
3  3     3
4  3     3
1  4     1
2  4     2
4  4     1

My goal is to create following matrix:

     [,1] [,2] [,3] [,4]
[1,]    1    2    1    NA
[2,]    1    2    3    3
[3,]   NA    3    3    3
[4,]    1    2    NA   1

I need the matrix as an input to evaluate inter-rater reliability with the IRR package. I tried to transpose via t() but I feel I need something like a pivot table that can handle the NA.

Thanks for your help!

Christopher
  • 2,120
  • 7
  • 31
  • 58

1 Answers1

1
x <- read.table(header=TRUE, text="id coder evaluation
1  1     1
2  1     2
3  1     1
1  2     1
2  2     2
3  2     3
4  2     3
2  3     3
3  3     3
4  3     3
1  4     1
2  4     2
4  4     1")

y <- matrix(NA, ncol=max(x$id), nrow=max(x$coder)) #Create empty matrix where ncol are taken from maximum of column id, and nrow from maximum of column coder
y[as.matrix(x[,2:1])] <- x[,3] #Fill the matix, where the positions are taken from x[,2:1] and the values come from x[,3]
#     [,1] [,2] [,3] [,4]
#[1,]    1    2    1   NA
#[2,]    1    2    3    3
#[3,]   NA    3    3    3
#[4,]    1    2   NA    1
GKi
  • 37,245
  • 2
  • 26
  • 48