stack2: Stack one Set of Variables from Wide to Long

View source: R/str2str_functions.R

stack2R Documentation

Stack one Set of Variables from Wide to Long

Description

stack2 converts one set of variables in a data.frame from wide to long format. (If you want to convert *multiple* sets of variables from wide to long, see reshape.) It is a modified version of stack that 1) adds a column for the rownames, 2) returns character vectors rather than factors, 3) can return additional (repeated) columns, and 4) can order by rownames original positions rather than the variable names being stacked call order.

Usage

stack2(
  data,
  select.nm,
  keep.nm = pick(x = names(data), val = select.nm, not = TRUE),
  rtn.el.nm = "el",
  rtn.vrbnames.nm = "vrb_names",
  rtn.rownames.nm = "row_names",
  order.by.rownames = TRUE,
  stringsAsFactors = FALSE
)

Arguments

data

data.frame of data.

select.nm

character vector of colnames from data specifying the variables to be stacked.

keep.nm

optional argument containing a character vector of colnames from data specifying the additional columns to be included in the return object. These columns are repeated down the data.frame as they are not stacked together. The default is the inclusion of all other columns in data other than select.nm. If NULL, then no other columns will be included.

rtn.el.nm

character vector of length 1 specifying the name of the column in the return object that corresponds to the elements of the stacked variables.

rtn.vrbnames.nm

character vector of length 1 specifying the name of the column in the return object that corresponds to the names of the stacked variables.

rtn.rownames.nm

character vector of length 1 specifying the name of the column in the return object that corresponds to the rownames.

order.by.rownames

logical vector of length 1 specifying whether the returned data.frame should be ordered by the positions of the rownames (TRUE) or by the positions of the names of the stacked variables (i.e., select.nm). Note, the ordering is by the *positions*, not by alphabetical order. If that is desired, convert the rownames to a (id) column and use reshape::melt.data.frame.

stringsAsFactors

logical vector of length 1 specifying whether the rtn.vrbnames.nm and rtn.rownames.nm columns should be converted to factors. Note, the factor levels are ordered by positions and not alphabetically (see v2fct).

Details

stack2 is also very similar to reshape::melt.data.frame. The differences are that it 1) adds a column for the rownames, 2) returns character vectors rather than factors, and 3) can order by rownames original positions rather than the variable names being stacked call order.

Value

data.frame with nrow = nrow(data) * length(`select.nm`) from stacking the elements of data[select.nm] on top of one another. The first column is the rownames with name rtn.rownames.nm, the second column is the names of the stacked variables with name rtn.vrbnames.nm, the third column is the stacked elements with name rtn.el.nm, and the additional columns are those specified by keep.nm.

See Also

unstack2 stack melt.data.frame

Examples


# general
stack2(data = mtcars, select.nm = c("disp","hp","drat","wt","qsec"),
   keep.nm = c("vs","am"))
stack2(data = mtcars, select.nm = c("disp","hp","drat","wt","qsec"),
   keep.nm = c("vs","am"), rtn.el.nm = "rating", rtn.vrbnames.nm = "item",
   rtn.rownames.nm = "row_names") # change the return object colnames
stack2(data = mtcars, select.nm = c("disp","hp","drat","wt","qsec"),
   keep.nm = pick(x = names(mtcars), val = c("disp","hp","drat","wt","qsec"),
   not = TRUE)) # include all columns from `data` in the return object

# keep options
stack2(data = mtcars, select.nm = c("mpg","cyl","disp")
   ) # default = keep all other variables in `data`
stack2(data = mtcars, select.nm = c("mpg","cyl","disp"), keep = c("gear","carb")
   ) # character vector = keep only specified variables in `data`
stack2(data = mtcars, select.nm = c("mpg","cyl","disp"), keep = NULL,
   ) # NULL = keep no other variables in `data`

# compare to utils:::stack.data.frame and reshape::melt.data.frame
ChickWeight2 <- as.data.frame(datasets::ChickWeight)
ChickWeight2$"Diet" <- as.integer(ChickWeight2$"Diet")
x <- stack(x = ChickWeight2, select = c("weight","Diet")) # does not allow
   # keeping additional columns
y <- reshape::melt(data = ChickWeight2, measure.vars = c("weight","Diet"),
   id.nm = c("Chick","Time"), variable_name = "vrb_names") # does not include
   # rownames and not ordered by rownames
z <- stack2(data = ChickWeight2, select.nm = c("weight","Diet"),
   keep.nm = c("Chick","Time"))
head(x); head(y); head(z)

str2str documentation built on Nov. 21, 2023, 1:08 a.m.