Description
The Unpivot function pivots data that is stored in columns into rows, which is
the reverse of the function Pivot (td_pivot_mle
).
Usage
td_unpivot_mle (
data = NULL,
unpivot = NULL,
input.types = FALSE,
attribute.column = "attribute",
value.column = "value_col",
accumulate = NULL,
data.sequence.column = NULL,
data.order.column = NULL
)
Arguments
data |
Required Argument. |
data.order.column |
Optional Argument. |
unpivot |
Required Argument. |
input.types |
Optional Argument.
Default Value: FALSE |
attribute.column |
Optional Argument. |
value.column |
Optional Argument. |
accumulate |
Required Argument. |
data.sequence.column |
Optional Argument. |
Value
Function returns an object of class "td_unpivot_mle" which is a named
list containing object of class "tbl_teradata".
Named list member can be referenced directly with the "$" operator
using name: result.
Examples
# Get the current context/connection
con <- td_get_context()$connection
# Load example data.
loadExampleData("unpivot_example", "unpivot_input")
# Create object(s) of class "tbl_teradata".
unpivot_input <- tbl(con, "unpivot_input")
# Example 1 : With "input.types" = FALSE there is only one value column and it has the
# data type VARCHAR and using default value for the arguments "attribute.column"
# and "value.column".
td_unpivot_out1 <- td_unpivot_mle(data = unpivot_input,
unpivot = c("temp","pressure","dewpoint"),
input.types = FALSE,
accumulate = c("sn","city","week")
)
# Example 2 : With "input.types" = TRUE, the output tbl_teradata has a separate
# column prefixed by "value.column" for each value in the unpivot argument
# (target column). The value colunms are suffixed by the datatype.
# Custom names are provided for "attribute.column" and "value.column" arguments.
td_unpivot_out2 <- td_unpivot_mle(data = unpivot_input,
unpivot = c("temp","pressure","dewpoint"),
input.types = TRUE,
attribute.column = "climate_attributes",
value.column = "attributevalue",
accumulate = c("sn","city","week")
)
# Example 3 : Use the output of the td_pivot_mle() function as an input to the
# td_unpivot_mle() function.
loadExampleData("pivot_example", "pivot_input")
pivot_input <- tbl(con, "pivot_input")
# Create a td_pivot_out object for all three keys in the pivot column.
td_pivot_out <- td_pivot_mle(data = pivot_input,
data.partition.column = c("sn","city","week"),
data.order.column = c("week","attribute"),
partition.columns = c("sn","city", "week"),
target.columns = c("value1"),
pivot.column = "attribute",
pivot.keys = c("temp","pressure","dewpoint")
)
# Use the tbl_teradata object td_pivot_out as input to the td_unpivot_mle() function.
td_unpivot_out3 <- td_unpivot_mle(data = td_pivot_out$result,
unpivot = c("value1_temp", "value1_pressure",
"value1_dewpoint"),
accumulate = c("sn","city","week"),
input.types = FALSE
)