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. Reshape from wide to long in R is also achieved using gather() and melt() function. Reshape from long to wide in R is also achieved using spread() and cast() function.
- Reshape from wide to long using reshape(), gather() and melt() function
- Reshape from long to wide using reshape(), spread() and dcast() function
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 “country” will be
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.
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
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
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
We have reshaped our sample data from long to wide format in R
You can also refer melting and casting in R
Wide to long using gather() function in R using tidyr package:
gather() function of tidyr package in R. gets the table name and the list of columns (population_in_million:gdp_percapita) to be reshaped from wide to long as shown below.
## reshape in R from wide to long example library(tidyr) data_long = gather(country, detail, value, population_in_million:gdp_percapita, factor_key=TRUE) data_long
so the resultant dataframe is reshaped from wide to long
Long to wide using spread() function in R using tidyr package:
spread() function of tidyr package in R. gets the table name and the list of columns (detail,value) to be reshaped from long to wide as shown below.
##### long to wide using spread() function of tidyr package library(tidyr) data_wide = spread(data_long, details, Value) data_wide
so the resultant dataframe is reshaped from long to wide
Wide to long using melt() function in R :
melt() function in R gets the table name and the list of columns to be kept constant (countries) is passed as argument in order to reshape from wide to long as shown below.
###### wide to long using melt function of reshape2() package in R country_w_to_L = melt(country, id.vars=c("countries")) country_w_to_L
so the resultant dataframe is reshaped from wide to long
Long to wide using dcast() function in R :
dcast() function in R gets the table name and the list of columns to be kept constant (countries) and list of columns to be reshaped (variable) is passed as argument in order to reshape from long to wide as shown below.
###### long to wide using cast function of reshape2() package in R country_L_to_W = dcast(country_w_to_L, countries~variable,sum) country_L_to_W
so the resultant dataframe is reshaped from long to wide