Skip to contents

This function "widens" data, increasing the number of columns and decreasing the number of rows. This is a dependency-free base-R equivalent of tidyr::pivot_wider().

Usage

data_to_wide(
  data,
  id_cols = NULL,
  values_from = "Value",
  names_from = "Name",
  names_sep = "_",
  names_prefix = "",
  names_glue = NULL,
  values_fill = NULL,
  verbose = TRUE,
  ...
)

reshape_wider(
  data,
  id_cols = NULL,
  values_from = "Value",
  names_from = "Name",
  names_sep = "_",
  names_prefix = "",
  names_glue = NULL,
  values_fill = NULL,
  verbose = TRUE,
  ...
)

Arguments

data

A data frame to pivot.

id_cols

The name of the column that identifies the rows. If NULL, it will use all the unique rows.

values_from

The name of the column that contains the values to be used as future variable values.

names_from

The name of the column that contains the levels to be used as future column names.

names_sep

If names_from or values_from contains multiple variables, this will be used to join their values together into a single string to use as a column name.

names_prefix

String added to the start of every variable name. This is particularly useful if names_from is a numeric vector and you want to create syntactic variable names.

names_glue

Instead of names_sep and names_prefix, you can supply a glue specification that uses the names_from columns to create custom column names. Note that the only delimiters supported by names_glue are curly brackets, { and }.

values_fill

Optionally, a (scalar) value that will be used to replace missing values in the new columns created.

verbose

Toggle warnings.

...

Not used for now.

Value

If a tibble was provided as input, reshape_wider() also returns a tibble. Otherwise, it returns a data frame.

See also

Examples

data_long <- read.table(header = TRUE, text = "
 subject sex condition measurement
       1   M   control         7.9
       1   M     cond1        12.3
       1   M     cond2        10.7
       2   F   control         6.3
       2   F     cond1        10.6
       2   F     cond2        11.1
       3   F   control         9.5
       3   F     cond1        13.1
       3   F     cond2        13.8
       4   M   control        11.5
       4   M     cond1        13.4
       4   M     cond2        12.9")


data_to_wide(
  data_long,
  id_cols = "subject",
  names_from = "condition",
  values_from = "measurement"
)
#>   subject control cond1 cond2
#> 1       1     7.9  12.3  10.7
#> 2       2     6.3  10.6  11.1
#> 3       3     9.5  13.1  13.8
#> 4       4    11.5  13.4  12.9

data_to_wide(
  data_long,
  id_cols = "subject",
  names_from = "condition",
  values_from = "measurement",
  names_prefix = "Var.",
  names_sep = "."
)
#>   subject Var.control Var.cond1 Var.cond2
#> 1       1         7.9      12.3      10.7
#> 2       2         6.3      10.6      11.1
#> 3       3         9.5      13.1      13.8
#> 4       4        11.5      13.4      12.9

production <- expand.grid(
  product = c("A", "B"),
  country = c("AI", "EI"),
  year = 2000:2014
)
production <- data_filter(production, (product == "A" & country == "AI") | product == "B")

production$production <- rnorm(nrow(production))

data_to_wide(
  production,
  names_from = c("product", "country"),
  values_from = "production",
  names_glue = "prod_{product}_{country}"
)
#>    year   prod_A_AI   prod_B_AI    prod_B_EI
#> 1  2000  1.22839278  0.27602348 -1.048975504
#> 2  2001 -0.52086934  1.62320252 -1.070068229
#> 3  2002  1.68588724 -0.24168977 -0.468200479
#> 4  2003 -0.77297823  2.14991934 -1.334353628
#> 5  2004  0.49587048  1.23397624  0.634362125
#> 6  2005  0.41202227  0.79358531 -0.152410633
#> 7  2006 -0.22889582 -0.90079175 -0.735026156
#> 8  2007 -1.42768578  0.61928353 -0.006198262
#> 9  2008 -0.68570685 -0.27933353 -0.782730275
#> 10 2009 -0.77899724 -0.37480009 -0.319393809
#> 11 2010  0.08454377 -0.76847360 -0.625910913
#> 12 2011 -0.90087086  0.66372867  0.300279118
#> 13 2012  0.07485682  0.20637270 -0.488922835
#> 14 2013 -0.62795166 -0.04691673  0.162618115
#> 15 2014  1.29230591 -0.46355650  0.305463227