How Can the cast() Function in R Be Used?

The cast() function in R is used to reshape data frames into different formats, such as converting from long to wide format or vice versa. This function can be applied to data with multiple variables and can also be used to aggregate data by grouping variables. It is particularly useful for organizing and summarizing large datasets, making it easier to analyze and visualize the data in a more meaningful way. By specifying the desired format and grouping variables, the cast() function allows for flexible and efficient data manipulation 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.

Related:

Additional Resources

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

x