fhir_melt: Melt multiple entries

View source: R/multiple_entries.R

fhir_meltR Documentation

Melt multiple entries

Description

This function divides multiple entries in an indexed data frame as produced by fhir_crack() into separate rows.

Usage

fhir_melt(
  indexed_data_frame,
  columns,
  brackets = c("<", ">"),
  sep = " ",
  id_name = "resource_identifier",
  all_columns = FALSE
)

Arguments

indexed_data_frame

A data.frame/data.table with indexed multiple entries.

columns

A character vector specifying the names of all columns that should be molten simultaneously. It is advisable to only melt columns simultaneously that belong to the same (repeating) attribute!

brackets

A character vector of length two, defining the brackets used for the indices.

sep

A character vector of length one defining the separator that was used when pasting together multiple entries in fhir_crack().

id_name

A character vector of length one, the name of the column that will hold the identification of the origin of the new rows.

all_columns

Return all columns? Defaults to FALSE, meaning only those specified in columns are returned.

Details

Every row containing values that consist of multiple entries on the variables specified by the argument columns will be turned into multiple rows, one for each entry. Values on other variables will be repeated in all the new rows.

The new data.frame will contain only the molten variables (if all_cloumns = FALSE) or all variables (if all_columns = TRUE) as well as an additional variable resource_identifier that maps which rows came from the same origin. The name of this column can be changed in the argument id_name.

For a more detailed description on how to use this function please see the corresponding package vignette.

Value

A data.frame/data.table where each entry from the variables in columns appears in a separate row.

See Also

fhir_common_columns(), fhir_rm_indices()

Examples

#unserialize example
bundles <- fhir_unserialize(bundles = example_bundles1)

#crack fhir resources
table_desc <- fhir_table_description(
    resource = "Patient",
    brackets = c("[", "]"),
    sep = " "
)

df <- fhir_crack(bundles = bundles, design = table_desc)

#find all column names associated with attribute address
col_names <- fhir_common_columns(df, "address")

#original data frame
df

#only keep address columns
fhir_melt(
     indexed_data_frame = df,
     columns            = col_names,
     brackets           = c("[", "]"),
     sep = " "
 )

#keep all columns
fhir_melt(indexed_data_frame = df, columns = col_names,
          brackets = c("[","]"), sep = " ", all_columns = TRUE)

POLAR-fhiR/fhircrackr documentation built on Dec. 24, 2024, 3:06 a.m.