Reshape Data


Codes for Reshaping Data

Published by Haerim Hwang

reshaping data long to wide wide to long data science r

0 min READ

  • These codes reshape data (a) from a long format to a wide format or (b) from a wide format to a long format.


  • You can download the sample dataset for practice.


  • Codes

    • Open the sample CSV file you downloaded from the above link

      data_summary_L2 <- read.csv(file.choose(), header = TRUE, stringsAsFactors = T)
      


    • Reshape data from a long format to a wide format

      data_summary_L2 <- select(data_summary_L2, participant, proficiency, condition, judgment)
      droplevels(data_summary_L2)
      data_L2_wide = spread(data_summary_L2, key = "condition", value = "judgment")
      


    • Reshape data from a wide format to a long format

      data_L2_long <- gather(data_L2_wide, condition, judgment, If_No_gap:Who_Gap, factor_key = TRUE)