multi_pivot_wider.RdTransforming data.frame with Complex Identifiers into Wide Format
multi_pivot_wider(data, id, param_from, value_from, drop_na = FALSE)(data.frame) to be pivoted.
(character) the name of the columns whose combination uniquely identify the observations.
(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.
(character) the name of the column containing the values that will populate the output.
(logical) should column containing only NAs be dropped.
data.frame in a wide format.
This function allows to identify observations on the basis of several columns. Warning: Instead of nesting duplicated values, the function will throw an error if the same parameter is provided twice for the same observation.
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)
)
multi_pivot_wider(test_data, c("the_obs", "the_obs2"), "the_param", "the_val")
#> the_obs the_obs2 gender height other weight
#> 1 A Ax M 165 <NA> 65
#> 2 B Bx F 166 <NA> 66
#> 3 C Cx <NA> 155 <NA> <NA>
#> 4 D Dx <NA> <NA> TRUE <NA>
multi_pivot_wider(test_data, "the_obs2", "the_param", "the_val")
#> the_obs2 gender height other weight
#> 1 Ax M 165 <NA> 65
#> 2 Bx F 166 <NA> 66
#> 3 Cx <NA> 155 <NA> <NA>
#> 4 Dx <NA> <NA> TRUE <NA>