Skip to contents

Pivot column data from long format to wide format.

Usage

cast(object, ...)

# S4 method for DFrame
cast(object, colnames = "colname", values = "value")

Arguments

object

Object.

colnames

character(1). Name of the column that contains the column name values in long format. Must define an unordered factor column.

values

character(1). Name of the column that contains the values in long format. Must define an atomic column that is not factor.

...

Additional arguments.

Value

DataFrame.

Note

Updated 2023-08-23.

See also

  • https://seananderson.ca/2013/10/19/reshape/

  • reshape2::dcast (deprecated).

  • tidyr::pivot_wider.

  • Python pandas.pivot_table.

  • Python pandas.wide_to_long.

  • Python pandas.DataFrame.pivot.

Examples

data(matrix, package = "AcidTest")

## DFrame ====
long <- melt(matrix)
print(long)
#> DataFrame with 16 rows and 3 columns
#>      rowname  colname     value
#>     <factor> <factor> <integer>
#> 1     gene01 sample01         1
#> 2     gene02 sample01         5
#> 3     gene03 sample01         9
#> 4     gene04 sample01        13
#> 5     gene01 sample02         2
#> ...      ...      ...       ...
#> 12    gene04 sample03        15
#> 13    gene01 sample04         4
#> 14    gene02 sample04         8
#> 15    gene03 sample04        12
#> 16    gene04 sample04        16
wide <- cast(long)
print(wide)
#> DataFrame with 4 rows and 4 columns
#>         sample01  sample02  sample03  sample04
#>        <integer> <integer> <integer> <integer>
#> gene01         1         2         3         4
#> gene02         5         6         7         8
#> gene03         9        10        11        12
#> gene04        13        14        15        16