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

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

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

问题

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

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

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

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

原始数据:

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
))

转置后的数据:

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 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.
https://stackoverflow.com/questions/9610917/analyzing-limesurvey-ranking-data-in-r

答案1

得分: 1

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

library(tidyr)

dat %>%
  pivot_longer(
    X1:X9,
    names_prefix = "X",
    names_transform = as.integer
  ) %>%
  pivot_wider(names_from = value, values_from = name)
# 一个 tibble: 6 × 10
     id `Parque Arví` `Jardín Botánico` `Parques del Río` `Parque de los Pies Descalzos`
  <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>
英文:

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 %&gt;% 
  pivot_longer(
    X1:X9, 
    names_prefix = &quot;X&quot;, 
    names_transform = as.integer
  ) %&gt;% 
  pivot_wider(names_from = value, values_from = name)
# A tibble: 6 &#215; 10
     id `Parque Arv&#237;` `Jard&#237;n Bot&#225;nico` `Parques del R&#237;o` Parque de los Pies D…&#185;
  &lt;int&gt;         &lt;int&gt;             &lt;int&gt;             &lt;int&gt;                  &lt;int&gt;
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: &#185;​`Parque de los Pies Descalzos`
# ℹ 5 more variables: `Parque de la Luz` &lt;int&gt;, `Parque Explora` &lt;int&gt;,
#   `Parque natural Cerro Volador` &lt;int&gt;, `Parque Norte` &lt;int&gt;,
#   `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:

确定