unstack2 {str2str}R Documentation

Unstack one Set of Variables from Long to Wide

Description

unstack2 converts one set of variables in a data.frame from long to wide format. (If you want to convert multiple sets of variables from long to wide, see reshape.) It is a modified version of unstack that 1) requires a column for the rownames of the data.frame (or equivalently an id column with unique values for each row in the wide format) before it was stacked, 2) can retain additional columns not being unstacked, and 3) can order by rownames original positions rather than their alphanumerical order.

Usage

unstack2(
  data,
  rownames.nm = "row_names",
  vrbnames.nm = "vrb_names",
  el.nm = "el",
  keep.nm = pick(x = names(data), val = c(rownames.nm, vrbnames.nm, el.nm), not = TRUE),
  add.missing = TRUE,
  rownamesAsColumn = FALSE
)

Arguments

data

data.frame of data containing stacked variables.

rownames.nm

character vector of length 1 specifying the colname in data for whom its unique values correspond to the rows in the return object.

vrbnames.nm

character vector of length 1 specifying the colname in `data` that contains the names of the variables to be unstacked.

el.nm

character vector of length 1 specifying the colname in data containing the elements from the variable to be unstacked.

keep.nm

optional argument containing a character vector of colnames from data specifying the additional columns to be included in the return object. The default is all the other columns in the data.frame besides rownames.nm, vrbnames.nm, and el.nm. If NULL, then no additional columns are retained. The keep.nm columns will be the last (aka most right) columns in the return object.

add.missing

logical vector of length 1 specifying whether missing values should be added when unstacking. This will occur if there are unequal number of rows for each variable in the set. If FALSE, an error will be returned when there are an unequal number of rows and missing values would need to be added to create the returned data.frame.

rownamesAsColumn

logical vector of length 1 specifying whether the unique values in rownames.nm column should be a column in the return object (TRUE) or the rownames of the return object (FALSE).

Details

unstack2 is also very similar to reshape::cast.data.frame. The differences are that it 1) can return the rownames as rownames of the returned data.frame rather than an id column, 2) can retain additional columns not being unstacked, and 3) can order by rownames original positions rather than the variable names being stacked call order.

Value

data.frame with nrow = length(unique(data[[rownames.nm]])) from unstacking the elements of el.nm alongside one another. New columns are created for each unique value in vrbnames.nm as well as columns for any colnames additional specified by keep.nm. If rownamesAsColumn = TRUE, then the first column is the unique values in rownames.nm; otherwise, they are the rownames of the return object (default).

See Also

stack2 unstack cast

Examples


# ordered by rownames
stacked <- stack2(data = mtcars, select.nm = c("disp","hp","drat","wt","qsec"),
   keep.nm = c("vs","am"), order.by.rownames = TRUE)
x <- unstack2(stacked)
# ordered by vrbnames
stacked <- stack2(data = mtcars, select.nm = c("disp","hp","drat","wt","qsec"),
   keep.nm = c("vs","am"), order.by.rownames = FALSE)
y <- unstack2(stacked)
identical(x, y)

# rownames as a column
z <- unstack2(data = stacked, rownamesAsColumn = TRUE)

# compare to utils:::unstack.data.frame and reshape::cast
stacked <- stack2(data = mtcars, select.nm = c("disp","hp","drat","wt","qsec"),
   keep.nm = c("vs","am"))
x <- unstack(x = stacked, form = el ~ vrb_names) # automatically sorts the colnames alphabetically
y <- reshape::cast(data = stacked, formula = row_names ~ vrb_names,
   value = "el") # automatically sorts the rownames alphabetically
z <- unstack2(stacked) # is able to keep additional variables
head(x); head(y); head(z)

# unequal number of rows for each unique value in `data`[[`vrbnames.nm`]]
# this can occur if you are using unstack2 without having called stack2 right before
row_keep <- sample(1:nrow(stacked), size = nrow(stacked) / 2)
stacked_rm <- stacked[row_keep, ]
unstack2(data = stacked_rm, rownames.nm = "row_names", vrbnames.nm = "vrb_names", el.nm = "el")
## Not run:  # error when `add.missing` = FALSE
   unstack2(data = stacked_rm, rownames.nm = "row_names", vrbnames.nm = "vrb_names",
      el.nm = "el", add.missing = FALSE)

## End(Not run)


[Package str2str version 1.0.0 Index]