Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reshape data from long to wide format

Tags:

r

r-faq

reshape

I'm having trouble rearranging the following data frame:

set.seed(45) dat1 <- data.frame(     name = rep(c("firstName", "secondName"), each=4),     numbers = rep(1:4, 2),     value = rnorm(8)     )  dat1        name  numbers      value 1  firstName       1  0.3407997 2  firstName       2 -0.7033403 3  firstName       3 -0.3795377 4  firstName       4 -0.7460474 5 secondName       1 -0.8981073 6 secondName       2 -0.3347941 7 secondName       3 -0.5013782 8 secondName       4 -0.1745357 

I want to reshape it so that each unique "name" variable is a rowname, with the "values" as observations along that row and the "numbers" as colnames. Sort of like this:

     name          1          2          3         4 1  firstName  0.3407997 -0.7033403 -0.3795377 -0.7460474 5 secondName -0.8981073 -0.3347941 -0.5013782 -0.1745357 

I've looked at melt and cast and a few other things, but none seem to do the job.

like image 317
Steve Avatar asked May 04 '11 22:05

Steve


People also ask

How do you reshape long to wide in Excel?

You want to reshape it to wide format. Press CTRL + SHIFT + ENTER to confirm this formula as it's an array formula. If this formula is entered correctly, you would see the formula inside the curly brackets {}. Column A does not necessarily to be in numeric format.

How do you reshape data from long to wide format in Python?

To summarize, if you need to reshape a Pandas dataframe from long to wide, use pd. pivot() . If you need to reshape a Pandas dataframe from wide to long, use pd. melt() .


2 Answers

Using reshape function:

reshape(dat1, idvar = "name", timevar = "numbers", direction = "wide") 
like image 86
Chase Avatar answered Sep 20 '22 19:09

Chase


The new (in 2014) tidyr package also does this simply, with gather()/spread() being the terms for melt/cast.

Edit: Now, in 2019, tidyr v 1.0 has launched and set spread and gather on a deprecation path, preferring instead pivot_wider and pivot_longer, which you can find described in this answer. Read on if you want a brief glimpse into the brief life of spread/gather.

library(tidyr) spread(dat1, key = numbers, value = value) 

From github,

tidyr is a reframing of reshape2 designed to accompany the tidy data framework, and to work hand-in-hand with magrittr and dplyr to build a solid pipeline for data analysis.

Just as reshape2 did less than reshape, tidyr does less than reshape2. It's designed specifically for tidying data, not the general reshaping that reshape2 does, or the general aggregation that reshape did. In particular, built-in methods only work for data frames, and tidyr provides no margins or aggregation.

like image 26
Gregor Thomas Avatar answered Sep 20 '22 19:09

Gregor Thomas