Convert a data.frame (or tibble) into a multidimensional array
Source:R/df_to_array.R
df_to_array.Rd
Convert a data.frame (or tibble) into a multidimensional array
Arguments
- df
a
data.frame()
(ortibble::tibble()
) in long format (each row contains one value for the intended array)- dim_var_names
a
character()
vector of variable names indf
. All of these variables should be factors, or a warning will be produced.- value_var_name
a
character()
variable containing a variable name fromdf
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