# 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)
Run the code above in your browser using DataLab