R Data Reshaping
Data reshaping in R involves transforming the structure of data frames using functions like gather()
, spread()
, melt()
, and dcast()
. These functions are essential for preparing data for analysis.
Key Topics
Wide to Long Format
# Using gather() from tidyr
library(tidyr)
wide_data <- data.frame(ID = 1:3, Q1 = c(10, 20, 30), Q2 = c(15, 25, 35))
long_data <- gather(wide_data, key = "Question", value = "Score", Q1:Q2)
print(long_data)
Output:
ID Question Score
1 Q1 10
2 Q1 20
3 Q1 30
1 Q2 15
2 Q2 25
3 Q2 35
1 Q1 10
2 Q1 20
3 Q1 30
1 Q2 15
2 Q2 25
3 Q2 35
Code Explanation: The gather()
function converts the wide-format data frame to long format by gathering the Q1
and Q2
columns.
Long to Wide Format
To reshape data from long to wide format, use functions like spread()
from the tidyr
package.
# Using spread() from tidyr
wide_data_again <- spread(long_data, key = "Question", value = "Score")
print(wide_data_again)
Output:
ID Q1 Q2
1 10 15
2 20 25
3 30 35
1 10 15
2 20 25
3 30 35
Code Explanation: The spread()
function reshapes the data back to wide format by spreading the Question
column into multiple columns.
Key Takeaways
- Use
gather()
to reshape data from wide to long format. - Use
spread()
to reshape data from long to wide format. - Data reshaping is essential for preparing data for different types of analysis.