loading...

dcast() transforms a data.trame from long to wide format, a little bit like tidyr::pivot_wider(). See data.table::dcast() for explanations.

dcast(
  data,
  formula,
  fun.aggregate = NULL,
  ...,
  margins = NULL,
  subset = NULL,
  fill = NULL,
  value.var = guess(data)
)

# S3 method for class 'data.trame'
dcast(
  data,
  formula,
  fun.aggregate = NULL,
  sep = "_",
  ...,
  margins = NULL,
  subset = NULL,
  fill = NULL,
  drop = TRUE,
  value.var = guess(data),
  verbose = getOption("datatable.verbose")
)

Arguments

data

A data.trame object.

formula

A formula LHS ~ RHS, see data.table::dcast() for details.

fun.aggregate

Te function to aggregate multiple data before dcasting.

...

Arguments passed to the aggregating function.

margins

Note implemented yet.

subset

Should the dcasting be done on a subset of the data?

fill

Value with which to fill missing cells.

value.var

The name of the column to use as value variable. If not provided it is "guessed" the guess() internal function is used to build a good default name.

sep

Character vector of length 1, used to separate parts of the variable names (_ by default).

drop

FALSE should dcast include all missing combinations?

verbose

Not used yet.

Value

A keyed data.trame is returned with the dcasted data.

Examples

# Adapted from first example of ?dcast.data.table
ChickWeight = as.data.trame(ChickWeight)
ChickWeight <- set_names(ChickWeight, tolower(names(ChickWeight)))
dtrm <- melt(ChickWeight, id.vars = 2:4)
dcast(dtrm, time ~ variable, fun.aggregate = mean)
#> # A data.trame: [12 × 2]
#> # Key:          time
#>     time weight
#>    <dbl>  <dbl>
#>  1     0   41.1
#>  2     2   49.2
#>  3     4   60.0
#>  4     6   74.3
#>  5     8   91.2
#>  6    10  108. 
#>  7    12  129. 
#>  8    14  144. 
#>  9    16  168. 
#> 10    18  190. 
#> 11    20  210. 
#> 12    21  219.