How to Use the cast() Function in R


You can use the cast() functions from the reshape2 package in R to convert a data frame from a long format to a wide format.

A wide format contains values that do not repeat in the first column.

A long format contains values that do repeat in the first column.

For example, consider the following two datasets that contain the exact same data expressed in different formats:

Wide vs. Long Data Format

The cast() function uses the following basic syntax to convert a data frame in a long format to a wide format:

cast(data, formula, fun.aggregate)

where:

  • data: Name of the data frame
  • formula: Specifies variables to cast into columns
  • fun.aggregate: An aggregate function to use (optional

Note: You should use the dcast function if you’d like the output to be a data frame or the acast function if you’d like the output to be a vector, matrix or array instead.

In the following example we’ll show how to use the dcast function to convert a data frame from a long format to a wide format.

Example: How to Use cast() in R

Suppose we have the following data frame in R that is currently in a long format:

#create data frame in long format
df <- data.frame(team=rep(c('A', 'B', 'C', 'D'), times=3),
                 variable=rep(c('points', 'assists', 'rebounds'), each=4),
                 points=c(88, 91, 99, 94, 12, 17, 24, 28, 22, 28, 30, 31))

#view data frame
df

   team variable points
1     A   points     88
2     B   points     91
3     C   points     99
4     D   points     94
5     A  assists     12
6     B  assists     17
7     C  assists     24
8     D  assists     28
9     A rebounds     22
10    B rebounds     28
11    C rebounds     30
12    D rebounds     31

We can use the dcast() function to quickly convert the data frame to a wide format:

library(reshape2)

#use cast() to convert data frame from long to wide format
wide_df <- dcast(df, team ~ variable)

#view wide data frame
wide_df

  team assists points rebounds
1    A      12     88       22
2    B      17     91       28
3    C      24     99       30
4    D      28     94       31

Notice that the data frame is now in a wide format.

Note: Since there was only once occurrence of each unique combination of team and variable, we did not need to specify an aggregate function to use when converting the data frame from a long to wide format.

Related: How to Use the melt() Function in R

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Filter Rows in R
How to Transpose a Data Frame in R
How to Rename Data Frame Columns in R

Leave a Reply

Your email address will not be published. Required fields are marked *