0

I have some ranking data colected in LimeSurvey. The data look like this:

The X1, X2...X9 is how the item was ranked.

structure(list(id = c(1L, 2L, 3L, 4L, 5L, 7L), X1 = c("Parque Arví", 
"Parque Explora", "Jardín Botánico", "Parque Explora", "Parque Arví", 
"Parque Arví"), X2 = c("Jardín Botánico", "Jardín Botánico", 
"Parque Explora", "Jardín Botánico", "Parques del Río", "Jardín Botánico"
), X3 = c("Parques del Río", "Parque Arví", "Parque natural Cerro Volador", 
"Parques del Río", "Jardín Botánico", "Parque de los Pies Descalzos"
), X4 = c("Parque de los Pies Descalzos", "Parque natural Cerro Volador", 
"Parque Arví", "Parque Norte", "Parque Llerás", "Parque Explora"
), X5 = c("Parque de la Luz", "Parque Llerás", "Parque Norte", 
"Parque natural Cerro Volador", "Parque Norte", "Parque natural Cerro Volador"
), X6 = c("Parque Explora", "Parque de los Pies Descalzos", "Parques del Río", 
"Parque de la Luz", "Parque Explora", "Parques del Río"), X7 = c("Parque natural Cerro Volador", 
"Parque Norte", "Parque de los Pies Descalzos", "Parque de los Pies Descalzos", 
"Parque de la Luz", "Parque de la Luz"), X8 = c("Parque Norte", 
"Parques del Río", "Parque de la Luz", "Parque Arví", "Parque de los Pies Descalzos", 
"Parque Norte"), X9 = c("Parque Llerás", "Parque de la Luz", 
"Parque Llerás", "Parque Llerás", "Parque natural Cerro Volador", 
"Parque Llerás")), class = "data.frame", row.names = c(NA, -6L
))

How can I transpose the data cell values in the original dataframe are converted into column names. The transposed dataframe should look like this:

structure(list(id = c(1L, 2L, 3L, 4L, 5L, 7L), Jardín.Botánico = c(2L, 
2L, 1L, 2L, 3L, 2L), Parque.Arví = c(1L, 3L, 4L, 8L, 1L, 1L), 
    Parque.de.la.Luz = c(5L, 9L, 8L, 6L, 7L, 7L), Parque.de.los.Pies.Descalzos = c(4L, 
    6L, 7L, 7L, 8L, 3L), Parque.Explora = c(6L, 1L, 2L, 1L, 6L, 
    4L), Parque.Llerás = c(9L, 5L, 9L, 9L, 4L, 9L), Parque.natural.Cerro.Volador = c(7L, 
    4L, 3L, 5L, 9L, 5L), Parque.Norte = c(8L, 7L, 5L, 4L, 5L, 
    8L), Parques.del.Río = c(3L, 8L, 6L, 3L, 2L, 6L)), class = "data.frame", row.names = c(NA, 
-6L))

I found this answer, but the solution does not show the column names, only V1, V2, V3, etc. Analyzing LimeSurvey ranking data in R

Chris
  • 353
  • 3
  • 9

1 Answers1

1

With tidyr, pivot longer, then back to wide, swapping values and names. When pivoting to long, use the names_prefix() argument to remove the Xs, and names_transform to convert to integer.

library(tidyr)

dat %>% 
  pivot_longer(
    X1:X9, 
    names_prefix = "X", 
    names_transform = as.integer
  ) %>% 
  pivot_wider(names_from = value, values_from = name)
# A tibble: 6 × 10
     id `Parque Arví` `Jardín Botánico` `Parques del Río` Parque de los Pies D…¹
  <int>         <int>             <int>             <int>                  <int>
1     1             1                 2                 3                      4
2     2             3                 2                 8                      6
3     3             4                 1                 6                      7
4     4             8                 2                 3                      7
5     5             1                 3                 2                      8
6     7             1                 2                 6                      3
# ℹ abbreviated name: ¹​`Parque de los Pies Descalzos`
# ℹ 5 more variables: `Parque de la Luz` <int>, `Parque Explora` <int>,
#   `Parque natural Cerro Volador` <int>, `Parque Norte` <int>,
#   `Parque Llerás` <int>
zephryl
  • 14,633
  • 3
  • 11
  • 30