Converting long to wide data in R
In the previous guide, we took a wide data set and converted it to long. While this conversion allowed us to apply some statistical and plotting functions on the data, there may be cases where doing the reverse, going from long to wide, may be useful.
id | Condition | Angle | Reaction_Time |
---|---|---|---|
1 | Absent | 0 | 420 |
1 | Absent | 4 | 420 |
1 | Absent | 8 | 480 |
1 | Present | 0 | 480 |
1 | Present | 4 | 600 |
1 | Present | 8 | 780 |
Converting Long to Wide
1. Unite the Condition and Angle columns
To convert our long format data to the original wide format data frame, we will need to chain a series of commands in the reverse order. Recall that our original wide data frame started out with columns that had the Condition and Angle together and that we had to separate those two variables. We will do the reverse here with the unite()
function. We will merge the Condition and Angle column data into one column called Condition.Angle.
wide.data <-
long.data %>%
unite(Condition.Angle, Condition, Angle, sep = "")
id | Condition.Angle | Reaction_Time |
---|---|---|
1 | Absent0 | 420 |
1 | Absent4 | 420 |
1 | Absent8 | 480 |
1 | Present0 | 480 |
1 | Present4 | 600 |
1 | Present8 | 780 |
2. Pivot the data
Next, we will use the pivot_wider()
function to arrange the values from the Reaction_Time column into separate columns names by the variables in the Condition.Angle columns.
wide.data <-
long.data %>%
unite(Condition.Angle, Condition, Angle, sep = "") %>%
pivot_wider(names_from = Condition.Angle, values_from = Reaction_Time)
id | Absent0 | Absent4 | Absent8 | Present0 | Present4 | Present8 |
---|---|---|---|---|---|---|
1 | 420 | 420 | 480 | 480 | 600 | 780 |
2 | 420 | 480 | 480 | 360 | 480 | 600 |
3 | 480 | 480 | 540 | 660 | 780 | 780 |
4 | 420 | 540 | 540 | 480 | 780 | 900 |
5 | 540 | 660 | 540 | 480 | 660 | 720 |
6 | 360 | 420 | 360 | 360 | 480 | 540 |
7 | 480 | 480 | 600 | 540 | 720 | 840 |
8 | 480 | 600 | 660 | 540 | 720 | 900 |
9 | 540 | 600 | 540 | 480 | 720 | 780 |
10 | 480 | 420 | 540 | 540 | 660 | 780 |
3. Remove the id column
Finally, I then selected all columns except id to end up with our original data frame.
wide.data <-
long.data %>%
unite(Condition.Angle, Condition, Angle, sep = "") %>%
pivot_wider(names_from = Condition.Angle, values_from = Reaction_Time) %>%
select(-id)
Absent0 | Absent4 | Absent8 | Present0 | Present4 | Present8 |
---|---|---|---|---|---|
420 | 420 | 480 | 480 | 600 | 780 |
420 | 480 | 480 | 360 | 480 | 600 |
480 | 480 | 540 | 660 | 780 | 780 |
420 | 540 | 540 | 480 | 780 | 900 |
540 | 660 | 540 | 480 | 660 | 720 |
360 | 420 | 360 | 360 | 480 | 540 |
480 | 480 | 600 | 540 | 720 | 840 |
480 | 600 | 660 | 540 | 720 | 900 |
540 | 600 | 540 | 480 | 720 | 780 |
480 | 420 | 540 | 540 | 660 | 780 |
References
Kassambara, Alboukadel. 2020a. Rstatix: Pipe-Friendly Framework for Basic Statistical Tests. https://CRAN.R-project.org/package=rstatix.
Maxwell, Scott, Harold Delaney, and Ken Kelley. 2020. AMCP: A Model Comparison Perspective. https://CRAN.R-project.org/package=AMCP.
Wickham, Hadley. 2019. Tidyverse: Easily Install and Load the ’Tidyverse’. https://CRAN.R-project.org/package=tidyverse.