Pivot wider a data frame while preserving labels.

pivot_wider_labels(
  df,
  names_from = "PARAMCD",
  labels_from = "PARAM",
  values_from = "AVAL",
  keep = "USUBJID"
)

Arguments

df

(data.frame) to pivot.

names_from

(string) the name of a column in df that will be used to name the columns of the pivoted data frame.

labels_from

(string) the name of a column in df that will be used to label the columns of the pivoted data frame.

values_from

(string) the name of a column in dfcontaining the values to be pivoted.

keep

(vector of strings) containing the name of the columns to be conserved in the pivoted data frame. Typically a unique identifier for the pivoted observations, such as USUBJID.

Value

(data.frame)

Examples

library(scda)
adsub <- synthetic_cdisc_data("rcd_2021_03_22")$adsub
pivot_wider_labels(adsub, "PARAMCD", "PARAM", "AVAL", c("USUBJID", "SUBJID"))
#> # A tibble: 400 × 7
#>    USUBJID               SUBJID BWGHTSI BHGHTSI BBMISI BECOG BBMRKR1
#>    <chr>                 <chr>    <dbl>   <dbl>  <dbl> <dbl>   <dbl>
#>  1 AB12345-BRA-1-id-105  id-105    62.5     1.6   24.5     3       1
#>  2 AB12345-BRA-1-id-134  id-134    98.8     1.5   44.3     3       1
#>  3 AB12345-BRA-1-id-141  id-141    49.6     1.2   36.6     1       2
#>  4 AB12345-BRA-1-id-236  id-236   162.      1.7   53.1     3       2
#>  5 AB12345-BRA-1-id-265  id-265   105.      1.7   38       2       1
#>  6 AB12345-BRA-1-id-42   id-42     53.8     1.7   18.1     4       2
#>  7 AB12345-BRA-1-id-65   id-65     60.6     1.6   24.8     3       2
#>  8 AB12345-BRA-1-id-93   id-93     63.8     1.6   26.2     4       1
#>  9 AB12345-BRA-11-id-171 id-171   144.      1.8   45.3     3       2
#> 10 AB12345-BRA-11-id-217 id-217    76.9     1.5   33.8     3       1
#> # … with 390 more rows