Home > Software design >  Wide to long form in R
Wide to long form in R

Time:11-13

Have a dataset for determining interrater reliability. Trying to restructure my data from wide to long form. Here is my data.

Subject   Rater   Item_1   Item_2
AB         1        6        4
AB         2        5        5
CD         1        4        5
CD         2        6        5
EF         1        4        4
EF         2        7        5

I want to restructure it so that it looks like this:

Subject   Item   Rater_1   Rater_2
AB         1         6         5
AB         2         4         5
CD         1         4         6
CD         2         5         5
EF         1         4         7
EF         2         4         5

I've tried pivot_longer but am unable to separate "rater" into two columns. Any ideas?

CodePudding user response:

Get the data in long format and use a different key to get it in wide format again.

library(dplyr)
library(tidyr)

#Thanks to @Dan Adams for the `NA` trick. 
df %>%
  pivot_longer(cols = starts_with('Item'), 
               names_to = c(NA, 'Item'), 
               names_sep = "_") %>%
  pivot_wider(names_from = Rater, values_from = value, names_prefix = "Rater_")

# Subject Item  Rater_1 Rater_2
#  <chr>   <chr>   <int>   <int>
#1 AB      1           6       5
#2 AB      2           4       5
#3 CD      1           4       6
#4 CD      2           5       5
#5 EF      1           4       7
#6 EF      2           4       5

data

df <- structure(list(Subject = c("AB", "AB", "CD", "CD", "EF", "EF"
), Rater = c(1L, 2L, 1L, 2L, 1L, 2L), Item_1 = c(6L, 5L, 4L, 
6L, 4L, 7L), Item_2 = c(4L, 5L, 5L, 5L, 4L, 5L)), 
class = "data.frame", row.names = c(NA, -6L))

CodePudding user response:

Here is a base R solution. You are really just transposing the data by group in this particular case.

Map(\(s) {
  x <- subset(df, df$Subject == s)
  x[,c("Item_1", "Item_2")] <-  t(x[,c("Item_1", "Item_2")])
  colnames(x) <- c("Subject", "Item", "Rater_1", "Rater_2")
  x
}, unique(df$Subject)) |>
  do.call(what = rbind)
#> # A tibble: 6 x 4
#>   Subject  Item Rater_1 Rater_2
#> * <chr>   <dbl>   <dbl>   <dbl>
#> 1 AB          1       6       5
#> 2 AB          2       4       5
#> 3 CD          1       4       6
#> 4 CD          2       5       5
#> 5 EF          1       4       7
#> 6 EF          2       4       5
  •  Tags:  
  • r
  • Related