如何转置排名数据并将单元格名称转换为列名称?

huangapple go评论130阅读模式
英文:

How to transpose ranking data and convert cell names into column names?

问题

我有一些在 LimeSurvey 中收集的排名数据。数据看起来像这样:

X1、X2...X9 是项目的排名方式。

如何将原始数据框中的数据单元格值转置为列名。转置后的数据框应如下所示:

我找到了这个答案,但解决方案没有显示列名,只有 V1、V2、V3 等。

原始数据:

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

转置后的数据:

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

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

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

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

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

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

I found this answer, but the solution does not show the column names, only V1, V2, V3, etc.
https://stackoverflow.com/questions/9610917/analyzing-limesurvey-ranking-data-in-r

答案1

得分: 1

使用tidyr,先进行长到宽的转换,然后再转回宽到长的形式,交换值和名称。在转换为长格式时,使用names_prefix()参数来移除X,并使用names_transform将其转换为整数。

  1. library(tidyr)
  2. dat %>%
  3. pivot_longer(
  4. X1:X9,
  5. names_prefix = "X",
  6. names_transform = as.integer
  7. ) %>%
  8. pivot_wider(names_from = value, values_from = name)
  1. # 一个 tibble: 6 × 10
  2. id `Parque Arví` `Jardín Botánico` `Parques del Río` `Parque de los Pies Descalzos`
  3. <int> <int> <int> <int> <int>
  4. 1 1 1 2 3 4
  5. 2 2 3 2 8 6
  6. 3 3 4 1 6 7
  7. 4 4 8 2 3 7
  8. 5 5 1 3 2 8
  9. 6 7 1 2 6 3
  10. # ℹ abbreviated name: `Parque de los Pies Descalzos`
  11. # ℹ 5 more variables: `Parque de la Luz` <int>, `Parque Explora` <int>,
  12. # `Parque natural Cerro Volador` <int>, `Parque Norte` <int>,
  13. # `Parque Llerás` <int>
英文:

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.

  1. library(tidyr)
  2. dat %&gt;%
  3. pivot_longer(
  4. X1:X9,
  5. names_prefix = &quot;X&quot;,
  6. names_transform = as.integer
  7. ) %&gt;%
  8. pivot_wider(names_from = value, values_from = name)
  1. # A tibble: 6 &#215; 10
  2. id `Parque Arv&#237;` `Jard&#237;n Bot&#225;nico` `Parques del R&#237;o` Parque de los Pies D…&#185;
  3. &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  4. 1 1 1 2 3 4
  5. 2 2 3 2 8 6
  6. 3 3 4 1 6 7
  7. 4 4 8 2 3 7
  8. 5 5 1 3 2 8
  9. 6 7 1 2 6 3
  10. # ℹ abbreviated name: &#185;​`Parque de los Pies Descalzos`
  11. # ℹ 5 more variables: `Parque de la Luz` &lt;int&gt;, `Parque Explora` &lt;int&gt;,
  12. # `Parque natural Cerro Volador` &lt;int&gt;, `Parque Norte` &lt;int&gt;,
  13. # `Parque Ller&#225;s` &lt;int&gt;

huangapple
  • 本文由 发表于 2023年5月28日 09:56:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定