Skip to contents

Convert a data.frame (or tibble) into a multidimensional array

Usage

df_to_array(df, dim_var_names, value_var_name = "value")

Arguments

df

a data.frame() (or tibble::tibble()) in long format (each row contains one value for the intended array)

dim_var_names

a character() vector of variable names in df. All of these variables should be factors, or a warning will be produced.

value_var_name

a character() variable containing a variable name from df which contains the values for the intended array.

Value

an array() with dimensions defined by the variables in df listed in dim_var_names

Examples

library(dplyr)
library(tidyr)
#> 
#> Attaching package: ‘tidyr’
#> The following object is masked from ‘package:magrittr’:
#> 
#>     extract

df <- iris %>%
  tidyr::pivot_longer(
    names_to = "parameter",
    cols = c("Sepal.Length", "Sepal.Width", "Petal.Width", "Petal.Length")
  ) %>%
  mutate(parameter = factor(parameter, levels = unique(parameter)))
arr <- df %>%
  serocalculator::df_to_array(
     dim_var_names = c("parameter", "Species"))
#> Error: 'df_to_array' is not an exported object from 'namespace:serocalculator'
ftable(arr[,,1:5])
#> Error: object 'arr' not found