【问题标题】:Is there an R function to reshape this data from long to wide?是否有一个 R 函数可以将这些数据从长到宽重塑?
【发布时间】:2020-05-22 01:16:59
【问题描述】:

数据现在的样子:

Coach ID | Student | score |
---------------------------------
1         | A      | 8     |
1         | B      | 3     |  
2         | A      | 5     |
2         | B      | 4     |
2         | C      | 7     |

看起来像这样:

Coach ID | Student | score | student_2|score_2| student_3|score_3
------------------------------------------------------------------
1         | A      | 8     | B        | 3     |   
2         | A      | 5     | B        | 4     | C        | 7

有没有办法将数据从长变宽?

谢谢!

【问题讨论】:

    标签: r excel reshape


    【解决方案1】:

    您可以为每个 student 创建一个具有唯一值的新标识符列,然后使用 pivot_wider 将多个列大小写为宽。

    library(dplyr)
    df %>%
      mutate(name = as.integer(factor(Student))) %>%
      tidyr::pivot_wider(names_from = name, values_from = c(Student, score))
    
    #  CoachID Student_1 Student_2 Student_3 score_1 score_2 score_3
    #    <int> <fct>     <fct>     <fct>       <int>   <int>   <int>
    #1       1 A         B         NA              8       3      NA
    #2       2 A         B         C               5       4       7
    

    数据

    df <- structure(list(CoachID = c(1L, 1L, 2L, 2L, 2L), Student = structure(c(1L, 
    2L, 1L, 2L, 3L), .Label = c("A", "B", "C"), class = "factor"), 
    score = c(8L, 3L, 5L, 4L, 7L)), class = "data.frame", row.names = c(NA, -5L))
    

    【讨论】:

      【解决方案2】:

      在基础 R 中,您可以使用 reshape 函数:

      reshape(transform(df,time = as.numeric(factor(Student))),idvar = "CoachID",dir = "wide",sep="_")
        CoachID Student_1 score_1 Student_2 score_2 Student_3 score_3
      1       1         A       8         B       3      <NA>      NA
      3       2         A       5         B       4         C       7
      

      【讨论】:

        【解决方案3】:
        library(tidyverse)
        
        mydf <- tribble(
          ~`Coach ID` , ~Student,  ~score,
          #  ---------------------------------
            1 ,"A" , 8, 
            1 , "B" , 3, 
            2 , "A" , 5,
            2 , "B" , 4,
            2 , "C" , 7
        )
        
        mydf <- mydf %>% 
          group_by(`Coach ID`) %>% 
          mutate(index = row_number())
        
        
        #----- Reshape to wide 
        
        mydf %>% 
          pivot_wider(
            id_cols = `Coach ID`,
            names_from = index,
            values_from = Student:score
            
          )
        
        

        【讨论】:

        • 使用唯一的 id 创建一个列,然后用新创建的变量重塑
        猜你喜欢
        • 2021-10-23
        • 1970-01-01
        • 2016-03-20
        • 2012-07-03
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        相关资源
        最近更新 更多