Reshape in R from wide to long and from long to wide

Reshape function in R transforms the data from wide to long and also transforms back the data from long to wide. Reshape in R – reshape(), is one of the efficient function to transform the data. We have discussed melting and casting in R which is another way of transforming data.

Data used for Reshaping from wide to long:

Let’s create a simple data frame to demonstrate our reshape example in R.

# create data frame

country<-data.frame(c("A","B","C"),c(100,200,120),c(2000,7000,15000))
colnames(country)<- c("countries","population_in_million","gdp_percapita")
country    

The data frame will be like

   countries        value.population_in_million      value.gdp_percapita

1         A                             100                                    2000

2         B                             200                                    7000

3         C                             120                                    15000

 

Reshape in R from wide to long:

We will reshape the above data frame from wide to long format in R. The above data frame is already in wide format.

reshape in r from wide to long

This can be accomplished with below code

## reshape in R from wide to long example

country_w_to_L<- reshape(data=country, idvar="countries",
                         varying = c("population_in_million","gdp_percapita"),
                         v.name=c("value"),
                         times=c("population_in_million","gdp_percapita"),
                         new.row.names = 1:1000,
                         direction="long")
  • data frame “country” is passed to reshape function
  • idvar is the variable which need to be left unaltered which is “countries”
  • varying are the ones that needs to converted from wide to long
  • v.names are the values that should be against the times in the resultant data frame.
  • new.row.names is used to assign row names to the resultant dataset
  • direction is, to which format the data needs to be transformed

so the output will be

   countries                  time                  value

1         A     population_in_million         100

2         B     population_in_million         200

3         C     population_in_million         120

4         A         gdp_percapita                 2000

5         B         gdp_percapita                 7000

6         C         gdp_percapita                 15000

We have reshaped our sample data from wide to long format in R

 

Reshape in R from long to wide:

We will reshape the above data frame from long to wide format in R as shown below

reshape in r from long to wide

This can be accomplished with below code

## reshape in R from long to wide example

country_L_to_w <- reshape(data=country_w_to_L,idvar="countries",
                          v.names = "value",
                          timevar = "time",
                          direction="wide")
  • data (country_w_to_L) which is in long format,  is passed to reshape function
  • idvar is the variable which need to be left unaltered, which is “countries”
  • timevar are the variables that needs to converted to wide format
  • v.names are the value variable
  • direction is, to which format the data needs to be transformed

so the output will be

  countries     value.population_in_million       value.gdp_percapita

1         A                             100                                    2000

2         B                             200                                    7000

3         C                             120                                    15000

We have reshaped our sample data from long to wide format in R

You can also refer melting and casting in R

previous small reshape in r                                                                                                                next small reshape in r