Transforming data.frame into Wide Format

mini_pivot_wider(data, id, param_from, value_from)

Arguments

data

(data.frame) to be pivoted.

id

(character) the name of the column identifying the observations. It will correspond to the row names of the output.

param_from

(character) the name of the column containing the names of the parameters to be pivoted. The unique values in this column will become column names in the output.

value_from

(character) the name of the column containing the values that will populate the output.

Value

data.frame in a wide format.

Details

instead of nesting duplicated values, the function will throw an error if the same parameter is provided twice for the same observation.

Examples

test_data <- data.frame(
  the_obs = c("A", "A", "A", "B", "B", "B", "C", "D"),
  the_obs2 = c("Ax", "Ax", "Ax", "Bx", "Bx", "Bx", "Cx", "Dx"),
  the_param = c("weight", "height", "gender", "weight", "gender", "height", "height", "other"),
  the_val = c(65, 165, "M", 66, "F", 166, 155, TRUE)
)

mini_pivot_wider(test_data, "the_obs", "the_param", "the_val")
#>   id gender height other weight
#> 1  A      M    165  <NA>     65
#> 2  B      F    166  <NA>     66
#> 3  C   <NA>    155  <NA>   <NA>
#> 4  D   <NA>   <NA>  TRUE   <NA>