pkgname <- "quest" source(file.path(R.home("share"), "R", "examples-header.R")) options(warn = 1) library('quest') base::assign(".oldSearch", base::search(), pos = 'CheckExEnv') base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv') cleanEx() nameEx("agg") ### * agg flush(stderr()); flush(stdout()) ### Name: agg ### Title: Aggregate an Atomic Vector by Group ### Aliases: agg ### ** Examples # one grouping variable agg(x = airquality$"Solar.R", grp = airquality$"Month", fun = mean) agg(x = airquality$"Solar.R", grp = airquality$"Month", fun = mean, na.rm = TRUE) # ignoring missing values agg(x = setNames(airquality$"Solar.R", nm = row.names(airquality)), grp = airquality$"Month", fun = mean, na.rm = TRUE) # keeps the names in the return object agg(x = airquality$"Solar.R", grp = airquality$"Month", rep = FALSE, fun = mean, na.rm = TRUE) # do NOT repeat aggregated values agg(x = airquality$"Solar.R", grp = airquality$"Month", rep = FALSE, rtn.grp = FALSE, fun = mean, na.rm = TRUE) # groups are the names of the returned atomic vector # two grouping variables tmp_nm <- c("vs","am") # Roxygen2 doesn't like a c() within a [] agg(x = mtcars$"mpg", grp = mtcars[tmp_nm], rep = TRUE, fun = sd) agg(x = mtcars$"mpg", grp = mtcars[tmp_nm], rep = FALSE, fun = sd) # do NOT repeat aggregated values agg(x = mtcars$"mpg", grp = mtcars[tmp_nm], rep = FALSE, rtn.grp = FALSE, fun = sd) # groups are the names of the returned atomic vector agg(x = mtcars$"mpg", grp = mtcars[tmp_nm], rep = FALSE, rtn.grp = FALSE, sep = ".", fun = sd) # change the separater for naming # error messages ## Not run: ##D agg(x = airquality$"Solar.R", grp = mtcars[tmp_nm]) # error returned ##D # b/c atomic vectors within \code{grp} not having the same length as \code{x} ## End(Not run) cleanEx() nameEx("agg_dfm") ### * agg_dfm flush(stderr()); flush(stdout()) ### Name: agg_dfm ### Title: Data Information by Group ### Aliases: agg_dfm ### ** Examples ### one grouping variable ## by in base R by(data = airquality[c("Ozone","Solar.R")], INDICES = airquality["Month"], simplify = FALSE, FUN = function(dat) cor(dat, use = "complete")[1,2]) ## rep = TRUE # rtn.group = TRUE agg_dfm(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month", rep = TRUE, rtn.grp = TRUE, fun = function(dat) cor(dat, use = "complete")[1,2]) # rtn.group = FALSE agg_dfm(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month", rep = TRUE, rtn.grp = FALSE, fun = function(dat) cor(dat, use = "complete")[1,2]) ## rep = FALSE # rtn.group = TRUE agg_dfm(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month", rep = FALSE, rtn.grp = TRUE, fun = function(dat) cor(dat, use = "complete")[1,2]) suppressWarnings(plyr::ddply(.data = airquality[c("Ozone","Solar.R","Month")], .variables = "Month", .fun = function(dat) cor(dat, use = "complete")[1,2])) # rtn.group = FALSE agg_dfm(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month", rep = FALSE, rtn.grp = FALSE, fun = function(dat) cor(dat, use = "complete")[1,2]) suppressWarnings(plyr::daply(.data = airquality[c("Ozone","Solar.R","Month")], .variables = "Month", .fun = function(dat) cor(dat, use = "complete")[1,2])) ### two grouping variables ## by in base R by(data = mtcars[c("mpg","cyl","disp")], INDICES = mtcars[c("vs","am")], FUN = nrow, simplify = FALSE) # with multiple group columns ## rep = TRUE # rtn.grp = TRUE agg_dfm(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"), rep = TRUE, rtn.grp = TRUE, fun = nrow) # rtn.grp = FALSE agg_dfm(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"), rep = TRUE, rtn.grp = FALSE, fun = nrow) ## rep = FALSE # rtn.grp = TRUE agg_dfm(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"), rep = FALSE, rtn.grp = TRUE, fun = nrow) agg_dfm(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"), rep = FALSE, rtn.grp = TRUE, rtn.result.nm = "value", fun = nrow) # rtn.grp = FALSE agg_dfm(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"), rep = FALSE, rtn.grp = FALSE, fun = nrow) agg_dfm(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"), rep = FALSE, rtn.grp = FALSE, sep = "_", fun = nrow) cleanEx() nameEx("aggs") ### * aggs flush(stderr()); flush(stdout()) ### Name: aggs ### Title: Aggregate Data by Group ### Aliases: aggs ### ** Examples aggs(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month", fun = mean, na.rm = TRUE) aggs(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month", rtn.grp = TRUE, fun = mean, na.rm = TRUE) # include the group columns aggs(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month", rep = FALSE, fun = mean, na.rm = TRUE) # do NOT repeat aggregated values aggs(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"), rep = FALSE, fun = mean, na.rm = TRUE) # with multiple group columns aggs(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"), rep = FALSE, rtn.grp = FALSE, fun = mean, na.rm = TRUE) # without returning groups cleanEx() nameEx("ave_dfm") ### * ave_dfm flush(stderr()); flush(stdout()) ### Name: ave_dfm ### Title: Repeated Group Statistics for a Data-Frame ### Aliases: ave_dfm ### ** Examples # one grouping variables ave_dfm(data = airquality, vrb.nm = c("Ozone","Solar.R"), grp.nm = "Month", fun = function(dat) cor(dat, use = "complete")[1,2]) # two grouping variables ave_dfm(data = mtcars, vrb.nm = c("mpg","cyl","disp"), grp.nm = c("vs","am"), fun = nrow) # with multiple group columns cleanEx() nameEx("by2") ### * by2 flush(stderr()); flush(stdout()) ### Name: by2 ### Title: Apply a Function to Data by Group ### Aliases: by2 ### ** Examples # one grouping variable by2(mtcars, .vrb.nm = c("mpg","cyl","disp"), .grp.nm = "vs", .fun = cov, use = "complete.obs") # two grouping variables x <- by2(mtcars, .vrb.nm = c("mpg","cyl","disp"), .grp.nm = c("vs","am"), .fun = cov, use = "complete.obs") print(x) str(x) # compare to by vrb_nm <- c("mpg","cyl","disp") # Roxygen runs the whole script if I put a c() in a [] grp_nm <- c("vs","am") # Roxygen runs the whole script if I put a c() in a [] y <- by(mtcars[vrb_nm], INDICES = mtcars[grp_nm], FUN = cov, use = "complete.obs", simplify = FALSE) str(y) # has dimnames rather than names cleanEx() nameEx("center") ### * center flush(stderr()); flush(stdout()) ### Name: center ### Title: Centering and/or Standardizing a Numeric Vector ### Aliases: center ### ** Examples center(x = mtcars$"disp") center(x = mtcars$"disp", scale = TRUE) center(x = mtcars$"disp", center = FALSE, scale = TRUE) center(x = setNames(mtcars$"disp", nm = row.names(mtcars))) cleanEx() nameEx("center_by") ### * center_by flush(stderr()); flush(stdout()) ### Name: center_by ### Title: Centering and/or Standardizing a Numeric Vector by Group ### Aliases: center_by ### ** Examples chick_data <- as.data.frame(ChickWeight) # because the "groupedData" class calls # `[.groupedData`, which is different than `[.data.frame` center_by(x = ChickWeight[["weight"]], grp = ChickWeight[["Chick"]]) center_by(x = setNames(obj = ChickWeight[["weight"]], nm = row.names(ChickWeight)), grp = ChickWeight[["Chick"]]) # with names tmp_nm <- c("Type","Treatment") # b/c Roxygen2 doesn't like a c() within a [] center_by(x = as.data.frame(CO2)[["uptake"]], grp = as.data.frame(CO2)[tmp_nm], scale = TRUE) # multiple grouping vectors cleanEx() nameEx("centers") ### * centers flush(stderr()); flush(stdout()) ### Name: centers ### Title: Centering and/or Standardizing Numeric Data ### Aliases: centers ### ** Examples centers(data = mtcars, vrb.nm = c("disp","hp","drat","wt","qsec")) centers(data = mtcars, vrb.nm = c("disp","hp","drat","wt","qsec"), scale = TRUE) centers(data = mtcars, vrb.nm = c("disp","hp","drat","wt","qsec"), center = FALSE, scale = TRUE) centers(data = mtcars, vrb.nm = c("disp","hp","drat","wt","qsec"), scale = TRUE, suffix = "_std") cleanEx() nameEx("centers_by") ### * centers_by flush(stderr()); flush(stdout()) ### Name: centers_by ### Title: Centering and/or Standardizing Numeric Data by Group ### Aliases: centers_by ### ** Examples ChickWeight2 <- as.data.frame(ChickWeight) # because the "groupedData" class calls # `[.groupedData`, which is different than `[.data.frame` row.names(ChickWeight2) <- as.numeric(row.names(ChickWeight)) / 1000 centers_by(data = ChickWeight2, vrb.nm = c("weight","Time"), grp.nm = "Chick") centers_by(data = ChickWeight2, vrb.nm = c("weight","Time"), grp.nm = "Chick", scale = TRUE, suffix = "_within") centers_by(data = as.data.frame(CO2), vrb.nm = c("conc","uptake"), grp.nm = c("Type","Treatment"), scale = TRUE) # multiple grouping columns cleanEx() nameEx("change") ### * change flush(stderr()); flush(stdout()) ### Name: change ### Title: Change Score from a Numeric Vector ### Aliases: change ### ** Examples change(x = attitude[[1]], n = -1L) # use L to prevent problems with floating point numbers change(x = attitude[[1]], n = -2L) # can specify any integer up to the length of `x` change(x = attitude[[1]], n = +1L) # can specify negative or positive integers change(x = attitude[[1]], n = +2L, undefined = -999) # user-specified indefined value change(x = attitude[[1]], n = -2L, undefined = -999) # user-specified indefined value change(x = attitude[[1]], n = 0L) # returns a vector of zeros ## Not run: ##D change(x = setNames(object = letters, nm = LETTERS), n = 3L) # character vector returns an error ## End(Not run) cleanEx() nameEx("change_by") ### * change_by flush(stderr()); flush(stdout()) ### Name: change_by ### Title: Change Scores from a Numeric Vector by Group ### Aliases: change_by ### ** Examples change_by(x = ChickWeight[["Time"]], grp = ChickWeight[["Chick"]], n = -1L) tmp_nm <- c("vs","am") # multiple grouping vectors change_by(x = mtcars[["disp"]], grp = mtcars[tmp_nm], n = +1L) tmp_nm <- c("Type","Treatment") # multiple grouping vectors change_by(x = as.data.frame(CO2)[["uptake"]], grp = as.data.frame(CO2)[tmp_nm], n = 2L) cleanEx() nameEx("changes") ### * changes flush(stderr()); flush(stdout()) ### Name: changes ### Title: Change Scores from Numeric Data ### Aliases: changes ### ** Examples changes(attitude, vrb.nm = names(attitude), n = -1L) # use L to prevent problems with floating point numbers changes(attitude, vrb.nm = names(attitude), n = -2L) # can specify any integer up to the length of `x` changes(attitude, vrb.nm = names(attitude), n = +1L) # can specify negative or positive integers changes(attitude, vrb.nm = names(attitude), n = +2L, undefined = -999) # user-specified indefined value changes(attitude, vrb.nm = names(attitude), n = -2L, undefined = -999) # user-specified indefined value ## Not run: ##D changes(str2str::d2d(InsectSprays), names(InsectSprays), ##D n = 3L) # character vector returns an error ## End(Not run) cleanEx() nameEx("changes_by") ### * changes_by flush(stderr()); flush(stdout()) ### Name: changes_by ### Title: Change Scores from Numeric Data by Group ### Aliases: changes_by ### ** Examples changes_by(data = ChickWeight, vrb.nm = c("weight","Time"), grp.nm = "Chick", n = -1L) changes_by(data = mtcars, vrb.nm = c("disp","mpg"), grp.nm = c("vs","am"), n = 1L) changes_by(data = as.data.frame(CO2), vrb.nm = c("conc","uptake"), grp.nm = c("Type","Treatment"), n = 2L) # multiple grouping columns cleanEx() nameEx("colMeans_if") ### * colMeans_if flush(stderr()); flush(stdout()) ### Name: colMeans_if ### Title: Column Means Conditional on Frequency of Observed Values ### Aliases: colMeans_if ### ** Examples colMeans_if(airquality) colMeans_if(x = airquality, ov.min = 150, prop = FALSE) cleanEx() nameEx("colNA") ### * colNA flush(stderr()); flush(stdout()) ### Name: colNA ### Title: Frequency of Missing Values by Column ### Aliases: colNA ### ** Examples colNA(as.matrix(airquality)) # count of missing values colNA(as.matrix(airquality), prop = TRUE) # proportion of missing values colNA(as.matrix(airquality), ov = TRUE) # count of observed values colNA(as.data.frame(airquality), prop = TRUE, ov = TRUE) # proportion of observed values cleanEx() nameEx("colSums_if") ### * colSums_if flush(stderr()); flush(stdout()) ### Name: colSums_if ### Title: Column Sums Conditional on Frequency of Observed Values ### Aliases: colSums_if ### ** Examples colSums_if(airquality) colSums_if(x = airquality, ov.min = 150, prop = FALSE) x <- data.frame("x" = c(1, 2, NA), "y" = c(1, NA, NA), "z" = c(NA, NA, NA)) colSums_if(x) colSums_if(x, ov.min = 0) colSums_if(x, ov.min = 0, allNA = 0) identical(x = colSums(x, na.rm = TRUE), y = colSums_if(x, impute = FALSE, ov.min = 0, allNA = 0)) # identical to # colSums(x, na.rm = TRUE) cleanEx() nameEx("decompose") ### * decompose flush(stderr()); flush(stdout()) ### Name: decompose ### Title: Decompose a Numeric Vector by Group ### Aliases: decompose ### ** Examples # single grouping variable chick_data <- as.data.frame(ChickWeight) # because the "groupedData" class # calls `[.groupedData`, which is different than `[.data.frame` decompose(x = ChickWeight[["weight"]], grp = ChickWeight[["Chick"]]) decompose(x = ChickWeight[["weight"]], grp = ChickWeight[["Chick"]], grand = FALSE) # no grand-mean centering decompose(x = setNames(obj = ChickWeight[["weight"]], nm = paste0(row.names(ChickWeight),"_row")), grp = ChickWeight[["Chick"]]) # with names # multiple grouping variables tmp_nm <- c("Type","Treatment") # b/c Roxygen2 doesn't like c() in a [] decompose(x = as.data.frame(CO2)[["uptake"]], grp = as.data.frame(CO2)[tmp_nm]) decompose(x = as.data.frame(CO2)[["uptake"]], grp = as.data.frame(CO2)[tmp_nm], n.shift = 1) decompose(x = as.data.frame(CO2)[["uptake"]], grp = as.data.frame(CO2)[tmp_nm], n.shift = c(+2, +1, -1, -2)) cleanEx() nameEx("decomposes") ### * decomposes flush(stderr()); flush(stdout()) ### Name: decomposes ### Title: Decompose Numeric Data by Group ### Aliases: decomposes ### ** Examples ChickWeight2 <- as.data.frame(ChickWeight) row.names(ChickWeight2) <- as.numeric(row.names(ChickWeight)) / 1000 decomposes(data = ChickWeight2, vrb.nm = c("weight","Time"), grp.nm = "Chick") decomposes(data = ChickWeight2, vrb.nm = c("weight","Time"), grp.nm = "Chick", suffix.wth = ".wth", suffix.btw = ".btw", suffix.grand = ".grand") decomposes(data = as.data.frame(CO2), vrb.nm = c("conc","uptake"), grp.nm = c("Type","Treatment")) # multiple grouping columns decomposes(data = as.data.frame(CO2), vrb.nm = c("conc","uptake"), grp.nm = c("Type","Treatment"), n.shift = 1) # with lead decomposes(data = as.data.frame(CO2), vrb.nm = c("conc","uptake"), grp.nm = c("Type","Treatment"), n.shift = c(+2, +1, -1, -2)) # with multiple lead/lags cleanEx() nameEx("dum2nom") ### * dum2nom flush(stderr()); flush(stdout()) ### Name: dum2nom ### Title: Dummy Variables to a Nominal Variable ### Aliases: dum2nom ### ** Examples dum <- data.frame( "Quebec_nonchilled" = ifelse(CO2$"Type" == "Quebec" & CO2$"Treatment" == "nonchilled", yes = 1L, no = 0L), "Quebec_chilled" = ifelse(CO2$"Type" == "Quebec" & CO2$"Treatment" == "chilled", yes = 1L, no = 0L), "Mississippi_nonchilled" = ifelse(CO2$"Type" == "Mississippi" & CO2$"Treatment" == "nonchilled", yes = 1L, no = 0L), "Mississippi_chilled" = ifelse(CO2$"Type" == "Mississippi" & CO2$"Treatment" == "chilled", yes = 1L, no = 0L) ) dum2nom(data = dum, dum.nm = names(dum)) # default dum2nom(data = dum, dum.nm = names(dum), rtn.fct = TRUE) # return as a factor ## Not run: ##D dum2nom(data = npk, dum.nm = c("N","P","K")) # error due to overlapping dummy columns ##D dum2nom(data = mtcars, dum.nm = c("vs","am"))# error due to overlapping dummy columns ## End(Not run) cleanEx() nameEx("freq") ### * freq flush(stderr()); flush(stdout()) ### Name: freq ### Title: Univariate Frequency Table ### Aliases: freq ### ** Examples freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = FALSE, sort = "frequency", decreasing = TRUE, na.last = TRUE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = FALSE, sort = "frequency", decreasing = TRUE, na.last = FALSE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = TRUE, sort = "frequency", decreasing = FALSE, na.last = TRUE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = TRUE, sort = "frequency", decreasing = FALSE, na.last = FALSE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = FALSE, sort = "position", decreasing = TRUE, na.last = TRUE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = FALSE, sort = "position", decreasing = TRUE, na.last = FALSE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = TRUE, sort = "position", decreasing = FALSE, na.last = TRUE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = TRUE, sort = "position", decreasing = FALSE, na.last = FALSE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = FALSE, sort = "alphanum", decreasing = TRUE, na.last = TRUE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = FALSE, sort = "alphanum", decreasing = TRUE, na.last = FALSE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = TRUE, sort = "alphanum", decreasing = FALSE, na.last = TRUE) freq(c(mtcars$"carb", NA, NA, mtcars$"gear"), prop = TRUE, sort = "alphanum", decreasing = FALSE, na.last = FALSE) cleanEx() nameEx("freq_by") ### * freq_by flush(stderr()); flush(stdout()) ### Name: freq_by ### Title: Univariate Frequency Table By Group ### Aliases: freq_by ### ** Examples x <- freq_by(mtcars$"gear", grp = mtcars$"vs") str(x) y <- freq_by(mtcars$"am", grp = mtcars$"vs", useNA = "no") str(y) str2str::lv2m(lapply(X = y, FUN = rev), along = 1) # ready to pass to prop.test() cleanEx() nameEx("freqs") ### * freqs flush(stderr()); flush(stdout()) ### Name: freqs ### Title: Multiple Univariate Frequency Tables ### Aliases: freqs ### ** Examples vrb_nm <- str2str::inbtw(names(psych::bfi), "A1","O5") freqs(data = psych::bfi, vrb.nm = vrb_nm) # default freqs(data = psych::bfi, vrb.nm = vrb_nm, prop = TRUE) # proportions by row freqs(data = psych::bfi, vrb.nm = vrb_nm, useNA = "no") # without NA counts freqs(data = psych::bfi, vrb.nm = vrb_nm, total = "yes") # include total counts cleanEx() nameEx("freqs_by") ### * freqs_by flush(stderr()); flush(stdout()) ### Name: freqs_by ### Title: Multiple Univariate Frequency Tables ### Aliases: freqs_by ### ** Examples vrb_nm <- str2str::inbtw(names(psych::bfi), "A1","O5") freqs_by(data = psych::bfi, vrb.nm = vrb_nm, grp.nm = "gender") # default freqs_by(data = psych::bfi, vrb.nm = vrb_nm, grp.nm = "gender", prop = TRUE) # proportions by row freqs_by(data = psych::bfi, vrb.nm = vrb_nm, grp.nm = "gender", useNA = "no") # without NA counts freqs_by(data = psych::bfi, vrb.nm = vrb_nm, grp.nm = "gender", total = "yes") # include total counts freqs_by(data = psych::bfi, vrb.nm = vrb_nm, grp.nm = c("gender","education")) # multiple grouping variables cleanEx() nameEx("long2wide") ### * long2wide flush(stderr()); flush(stdout()) ### Name: long2wide ### Title: Reshape Multiple Scores From Long to Wide ### Aliases: long2wide ### ** Examples # SINGLE GROUPING VARIABLE dat_long <- as.data.frame(ChickWeight) # b/c groupedData class does weird things... w1 <- long2wide(data = dat_long, vrb.nm = "weight", grp.nm = "Chick", obs.nm = "Time") # NAs inserted for missing observations in some groups w2 <- long2wide(data = dat_long, vrb.nm = "weight", grp.nm = "Chick", obs.nm = "Time", sep = "_") head(w1); head(w2) w3 <- long2wide(data = dat_long, vrb.nm = "weight", grp.nm = "Chick", obs.nm = "Time", sep = "_T", keep.attr = TRUE) attributes(w3) # MULTIPLE GROUPING VARIABLE tmp <- psychTools::sai grps <- interaction(tmp[1:3], drop = TRUE) dups <- duplicated(grps) dat_long <- tmp[!(dups), ] # for some reason there are duplicate groups in the data vrb_nm <- str2str::pick(names(dat_long), val = c("study","time","id"), not = TRUE) w4 <- long2wide(data = dat_long, vrb.nm = vrb_nm, grp.nm = c("study","id"), obs.nm = "time") w5 <- long2wide(data = dat_long, vrb.nm = vrb_nm, grp.nm = c("study","id"), obs.nm = "time", colnames.by.obs = FALSE) # colnames sorted by `vrb.nm` instead head(w4); head(w5) cleanEx() nameEx("make.dumNA") ### * make.dumNA flush(stderr()); flush(stdout()) ### Name: make.dumNA ### Title: Make Dummy Columns For Missing Data. ### Aliases: make.dumNA ### ** Examples make.dumNA(data = airquality, vrb.nm = c("Ozone","Solar.R")) make.dumNA(data = airquality, vrb.nm = c("Ozone","Solar.R"), rtn.lgl = TRUE) # logical vectors returned make.dumNA(data = airquality, vrb.nm = c("Ozone","Solar.R"), ov = TRUE, suffix = "_o") # 1 = observed value cleanEx() nameEx("make.dummy") ### * make.dummy flush(stderr()); flush(stdout()) ### Name: make.dummy ### Title: Make Dummy Columns ### Aliases: make.dummy ### ** Examples make.dummy(attitude$"rating" > 50) # ugly colnames make.dummy("rating_50plus" = attitude$"rating" > 50, "advance_50minus" = attitude$"advance" < 50) make.dummy("rating_50plus" = attitude$"rating" > 50, "advance_50minus" = attitude$"advance" < 50, rtn.lgl = TRUE) ## Not run: ##D make.dummy("rating_50plus" = attitude$"rating" > 50, ##D "mpg_20plus" = mtcars$"mpg" > 20) ## End(Not run) cleanEx() nameEx("make.fun_if") ### * make.fun_if flush(stderr()); flush(stdout()) ### Name: make.fun_if ### Title: Make a Function Conditional on Frequency of Observed Values ### Aliases: make.fun_if ### ** Examples # SD sd_if <- make.fun_if(fun = sd, na.rm = TRUE) # always have na.rm = TRUE sd_if(x = airquality[[1]], ov.min = .75) # proportion of observed values sd_if(x = airquality[[1]], ov.min = 116, prop = FALSE) # count of observed values sd_if(x = airquality[[1]], ov.min = 116, prop = FALSE, inclusive = FALSE) # not include ov.min values itself # skewness skew_if <- make.fun_if(fun = psych::skew, type = 1) # always have type = 1 skew_if(x = airquality[[1]], ov.min = .75) # proportion of observed values skew_if(x = airquality[[1]], ov.min = 116, prop = FALSE) # count of observed values skew_if(x = airquality[[1]], ov.min = 116, prop = FALSE, inclusive = FALSE) # not include ov.min values itself # mode popular <- function(x) names(sort(table(x), decreasing = TRUE))[1] popular_if <- make.fun_if(fun = popular) # works with character vectors too popular_if(x = c(unlist(dimnames(HairEyeColor)), rep.int(x = NA, times = 10)), ov.min = .50) popular_if(x = c(unlist(dimnames(HairEyeColor)), rep.int(x = NA, times = 10)), ov.min = .60) cleanEx() nameEx("make.product") ### * make.product flush(stderr()); flush(stdout()) ### Name: make.product ### Title: Make Product Terms (e.g., interactions) ### Aliases: make.product ### ** Examples make.product(data = attitude, x.nm = c("complaints","privileges"), m.nm = "learning", center.x = TRUE, center.m = TRUE, suffix.x = "_c", suffix.m = "_c") # with grand-mean centering make.product(data = attitude, x.nm = c("complaints","privileges"), m.nm = c("learning","raises"), combo = TRUE) # all possible combinations make.product(data = attitude, x.nm = c("complaints","privileges"), m.nm = c("learning","raises"), combo = FALSE) # only combinations "in parallel" cleanEx() nameEx("mean_if") ### * mean_if flush(stderr()); flush(stdout()) ### Name: mean_if ### Title: Mean Conditional on Minimum Frequency of Observed Values ### Aliases: mean_if ### ** Examples mean_if(x = airquality[[1]], ov.min = .75) # proportion of observed values mean_if(x = airquality[[1]], ov.min = 116, prop = FALSE) # count of observe values mean_if(x = airquality[[1]], ov.min = 116, prop = FALSE, inclusive = FALSE) # not include ov.min value itself mean_if(x = c(TRUE, NA, FALSE, NA), ov.min = .50) # works with logical vectors as well as numeric cleanEx() nameEx("mode2") ### * mode2 flush(stderr()); flush(stdout()) ### Name: mode2 ### Title: Statistical Mode of a Numeric Vector ### Aliases: mode2 ### ** Examples # ONE MODE vec <- c(7,8,9,7,8,9,9) mode2(vec) mode2(vec, multiple = TRUE) # TWO MODES vec <- c(7,8,9,7,8,9,8,9) mode2(vec) mode2(vec, multiple = TRUE) # WITH NA vec <- c(7,8,9,7,8,9,NA,9) mode2(vec) mode2(vec, na.rm = TRUE) vec <- c(7,8,9,7,8,9,NA,9,NA,NA) mode2(vec) mode2(vec, multiple = TRUE) cleanEx() nameEx("ncases") ### * ncases flush(stderr()); flush(stdout()) ### Name: ncases ### Title: Number of Cases in Data ### Aliases: ncases ### ** Examples vrb_nm <- c("Ozone","Solar.R","Wind") nrow(airquality[vrb_nm]) # number of cases regardless of missing data sum(complete.cases(airquality[vrb_nm])) # number of complete cases ncases(data = airquality, vrb.nm = c("Ozone","Solar.R","Wind"), ov.min = 2/3) # number of rows with at least 2 of the 3 variables observed cleanEx() nameEx("nom2dum") ### * nom2dum flush(stderr()); flush(stdout()) ### Name: nom2dum ### Title: Nominal Variable to Dummy Variables ### Aliases: nom2dum ### ** Examples nom2dum(infert$"education") # default nom2dum(infert$"education", prefix = "edu_") # use of the `prefix` argument nom2dum(nom = infert$"education", yes = "one", no = "zero", rtn.fct = TRUE) # returns factor columns cleanEx() nameEx("partial.cases") ### * partial.cases flush(stderr()); flush(stdout()) ### Name: partial.cases ### Title: Find Partial Cases ### Aliases: partial.cases ### ** Examples cases2keep <- partial.cases(data = airquality, vrb.nm = c("Ozone","Solar.R","Wind"), ov.min = .66) airquality2 <- airquality[cases2keep, ] # all cases with 2/3 variables observed cases2keep <- partial.cases(data = airquality, vrb.nm = c("Ozone","Solar.R","Wind"), ov.min = 1, prop = TRUE, inclusive = TRUE) complete_cases <- complete.cases(airquality) identical(x = unname(cases2keep), y = complete_cases) # partial.cases(ov.min = 1, prop = TRUE, # inclusive = TRUE) = complete.cases() cleanEx() nameEx("pomp") ### * pomp flush(stderr()); flush(stdout()) ### Name: pomp ### Title: Recode a Numeric Vector to Percentage of Maximum Possible (POMP) ### Units ### Aliases: pomp ### ** Examples vec <- psych::bfi[[1]] pomp(x = vec, mini = 1, maxi = 6) # absolute POMP units pomp(x = vec, relative = TRUE) # relative POMP units pomp(x = vec, mini = 1, maxi = 6, unit = 100) # unit = 100 pomp(x = vec, mini = 1, maxi = 6, unit = 50) # unit = 50 cleanEx() nameEx("pomps") ### * pomps flush(stderr()); flush(stdout()) ### Name: pomps ### Title: Recode Numeric Data to Percentage of Maximum Possible (POMP) ### Units ### Aliases: pomps ### ** Examples vrb_nm <- names(psych::bfi)[grepl(pattern = "A", x = names(psych::bfi))] pomps(data = psych::bfi, vrb.nm = vrb_nm, min = 1, max = 6) # absolute POMP units pomps(data = psych::bfi, vrb.nm = vrb_nm, relative = TRUE) # relative POMP units pomps(data = psych::bfi, vrb.nm = vrb_nm, min = 1, max = 6, unit = 100) # unit = 100 pomps(data = psych::bfi, vrb.nm = vrb_nm, min = 1, max = 6, unit = 50) # unit = 50 pomps(data = psych::bfi, vrb.nm = vrb_nm, min = 1, max = 6, suffix = "_pomp") cleanEx() nameEx("recode2other") ### * recode2other flush(stderr()); flush(stdout()) ### Name: recode2other ### Title: Recode Unique Values in a Character Vector to 0ther (or NA) ### Aliases: recode2other ### ** Examples # based on minimum frequency unique values state_region <- as.character(state.region) recode2other(state_region, freq.min = 13) # freq.min as a count recode2other(state_region, freq.min = 0.26, prop = TRUE) # freq.min as a proportion recode2other(state_region, freq.min = 13, other.nm = "_blank_") recode2other(state_region, freq.min = 13, other.nm = NA) # allows for other.nm to be NA recode2other(state_region, freq.min = 13, extra.nm = "South") # add an extra unique value to recode recode2other(state_region, freq.min = 13, inclusive = FALSE) # recodes "West" to "other" # based on user given unique values recode2other(state_region, freq.min = 0, extra.nm = c("South","West")) # recodes manually rather than by freq.min # current version does NOT allow for NA to be a unique value that is converted to other state_region2 <- c(NA, state_region, NA) recode2other(state_region2, freq.min = 13) # NA remains in the character vector recode2other(state_region2, freq.min = 0, extra.nm = c("South","West",NA)) # NA remains in the character vector cleanEx() nameEx("recodes") ### * recodes flush(stderr()); flush(stdout()) ### Name: recodes ### Title: Recode Data ### Aliases: recodes ### ** Examples recodes(data = psych::bfi, vrb.nm = c("A1","C4","C5","E1","E2","O2","O5"), recodes = "1=6; 2=5; 3=4; 4=3; 5=2; 6=1") re_codes <- "'Quebec' = 'canada'; 'Mississippi' = 'usa'; 'nonchilled' = 'no'; 'chilled' = 'yes'" recodes(data = CO2, vrb.nm = c("Type","Treatment"), recodes = re_codes, as.factor = FALSE) # convert from factors to characters cleanEx() nameEx("renames") ### * renames flush(stderr()); flush(stdout()) ### Name: renames ### Title: Rename Data Columns from a Codebook ### Aliases: renames ### ** Examples code_book <- list( data.frame("old" = c("rating","complaints"), "new" = c("RATING","COMPLAINTS")), data.frame("old" = c("privileges","learning"), "new" = c("PRIVILEGES","LEARNING")) ) renames(data = attitude, codebook = code_book, old = "old", new = "new") cleanEx() nameEx("reorders") ### * reorders flush(stderr()); flush(stdout()) ### Name: reorders ### Title: Reorder Levels of Factor Data ### Aliases: reorders ### ** Examples # factor vector reorder(x = state.region, X = state.region, FUN = length) # least frequent to most frequent reorder(x = state.region, X = state.region, FUN = function(vec) {-1 * length(vec)}) # most frequent to least frequent # data.frame of factors infert_fct <- infert fct_nm <- c("education","parity","induced","case","spontaneous") infert_fct[fct_nm] <- lapply(X = infert[fct_nm], FUN = as.factor) x <- reorders(data = infert_fct, fct.nm = fct_nm, fun = length) # least frequent to most frequent lapply(X = x, FUN = levels) y <- reorders(data = infert_fct, fct.nm = fct_nm, fun = function(vec) {-1 * length(vec)}) # most frequent to least frequent lapply(X = y, FUN = levels) # ord.nm specified as a different column in data.frame z <- reorders(data = infert_fct, fct.nm = fct_nm, ord.nm = "pooled.stratum", fun = mean) # category with highest mean for pooled.stratum to # category with lowest mean for pooled.stratum lapply(X = z, FUN = levels) cleanEx() nameEx("revalid") ### * revalid flush(stderr()); flush(stdout()) ### Name: revalid ### Title: Recode Invalid Values from a Vector ### Aliases: revalid ### ** Examples revalid(x = attitude[[1]], valid = 25:75, undefined = NA) # numeric vector revalid(x = as.character(ToothGrowth[["supp"]]), valid = c('VC'), undefined = NA) # character vector revalid(x = ToothGrowth[["supp"]], valid = c('VC'), undefined = NA) # factor cleanEx() nameEx("revalids") ### * revalids flush(stderr()); flush(stdout()) ### Name: revalids ### Title: Recode Invalid Values from Data ### Aliases: revalids ### ** Examples revalids(data = attitude, vrb.nm = names(attitude), valid = 25:75) # numeric data revalids(data = as.data.frame(CO2), vrb.nm = c("Type","Treatment"), valid = c('Quebec','nonchilled')) # factors cleanEx() nameEx("reverse") ### * reverse flush(stderr()); flush(stdout()) ### Name: reverse ### Title: Reverse Code a Numeric Vector ### Aliases: reverse ### ** Examples x <- psych::bfi[[1]] head(x, n = 15) y <- reverse(x = psych::bfi[[1]], min = 1, max = 6) head(y, n = 15) cor(x, y, use = "complete.obs") cleanEx() nameEx("reverses") ### * reverses flush(stderr()); flush(stdout()) ### Name: reverses ### Title: Reverse Code Numeric Data ### Aliases: reverses ### ** Examples tmp <- !(is.element(el = names(psych::bfi) , set = c("gender","education","age"))) vrb_nm <- names(psych::bfi)[tmp] reverses(data = psych::bfi, vrb.nm = vrb_nm, mini = 1, maxi = 6) cleanEx() nameEx("rowMeans_if") ### * rowMeans_if flush(stderr()); flush(stdout()) ### Name: rowMeans_if ### Title: Row Means Conditional on Frequency of Observed Values ### Aliases: rowMeans_if ### ** Examples rowMeans_if(airquality) rowMeans_if(x = airquality, ov.min = 5, prop = FALSE) cleanEx() nameEx("rowNA") ### * rowNA flush(stderr()); flush(stdout()) ### Name: rowNA ### Title: Frequency of Missing Values by Row ### Aliases: rowNA ### ** Examples rowNA(as.matrix(airquality)) # count of missing values rowNA(as.data.frame(airquality)) # with rownames rowNA(as.matrix(airquality), prop = TRUE) # proportion of missing values rowNA(as.matrix(airquality), ov = TRUE) # count of observed values rowNA(as.data.frame(airquality), prop = TRUE, ov = TRUE) # proportion of observed values cleanEx() nameEx("rowSums_if") ### * rowSums_if flush(stderr()); flush(stdout()) ### Name: rowSums_if ### Title: Row Sums Conditional on Frequency of Observed Values ### Aliases: rowSums_if ### ** Examples rowSums_if(airquality) rowSums_if(x = airquality, ov.min = 5, prop = FALSE) x <- data.frame("x" = c(1, 1, NA), "y" = c(2, NA, NA), "z" = c(NA, NA, NA)) rowSums_if(x) rowSums_if(x, ov.min = 0) rowSums_if(x, ov.min = 0, allNA = 0) identical(x = rowSums(x, na.rm = TRUE), y = unname(rowSums_if(x, impute = FALSE, ov.min = 0, allNA = 0))) # identical to # rowSums(x, na.rm = TRUE) cleanEx() nameEx("rowsNA") ### * rowsNA flush(stderr()); flush(stdout()) ### Name: rowsNA ### Title: Frequency of Multiple Sets of Missing Values by Row ### Aliases: rowsNA ### ** Examples vrb_list <- lapply(X = c("O","C","E","A","N"), FUN = function(chr) { tmp <- grepl(pattern = chr, x = names(psych::bfi)) names(psych::bfi)[tmp] }) rowsNA(data = psych::bfi, vrb.nm.list = vrb_list) # names set to first elements in `vrb.nm.list`[[i]] names(vrb_list) <- paste0(c("O","C","E","A","N"), "_m") rowsNA(data = psych::bfi, vrb.nm.list = vrb_list) # names set to names(`vrb.nm.list`) cleanEx() nameEx("score") ### * score flush(stderr()); flush(stdout()) ### Name: score ### Title: Observed Unweighted Scoring of a Set of Variables/Items ### Aliases: score ### ** Examples score(data = attitude, vrb.nm = c("complaints","privileges","learning","raises")) score(data = attitude, vrb.nm = c("complaints","privileges","learning","raises"), std = TRUE) # standardized scoring score(data = airquality, vrb.nm = c("Ozone","Solar.R","Temp"), ov.min = 0.75) # conditional on observed values cleanEx() nameEx("scores") ### * scores flush(stderr()); flush(stdout()) ### Name: scores ### Title: Observed Unweighted Scoring of Multiple Sets of Variables/Items ### Aliases: scores ### ** Examples list_colnames <- list("first" = c("rating","complaints","privileges"), "second" = c("learning","raises","critical")) scores(data = attitude, vrb.nm.list = list_colnames) list_colnames <- list("first" = c("Ozone","Wind"), "second" = c("Solar.R","Temp")) scores(data = airquality, vrb.nm.list = list_colnames, ov.min = .50, inclusive = FALSE) # scoring conditional on observed values cleanEx() nameEx("shift") ### * shift flush(stderr()); flush(stdout()) ### Name: shift ### Title: Shift a Vector (i.e., lag/lead) ### Aliases: shift ### ** Examples shift(x = attitude[[1]], n = -1L) # use L to prevent problems with floating point numbers shift(x = attitude[[1]], n = -2L) # can specify any integer up to the length of `x` shift(x = attitude[[1]], n = +1L) # can specify negative or positive integers shift(x = attitude[[1]], n = +2L, undefined = -999) # user-specified indefined value shift(x = setNames(object = letters, nm = LETTERS), n = 3L) # names are kept cleanEx() nameEx("shift_by") ### * shift_by flush(stderr()); flush(stdout()) ### Name: shift_by ### Title: Shift a Vector (i.e., lag/lead) by Group ### Aliases: shift_by ### ** Examples shift_by(x = ChickWeight[["Time"]], grp = ChickWeight[["Chick"]], n = -1L) tmp_nm <- c("vs","am") # b/c Roxygen2 doesn't like c() in a [] shift_by(x = mtcars[["disp"]], grp = mtcars[tmp_nm], n = 1L) tmp_nm <- c("Type","Treatment") # b/c Roxygen2 doesn't like c() in a [] shift_by(x = as.data.frame(CO2)[["uptake"]], grp = as.data.frame(CO2)[tmp_nm], n = 2L) # multiple grouping vectors cleanEx() nameEx("shifts") ### * shifts flush(stderr()); flush(stdout()) ### Name: shifts ### Title: Shift Data (i.e., lag/lead) ### Aliases: shifts ### ** Examples shifts(data = attitude, vrb.nm = colnames(attitude), n = -1L) shifts(data = mtcars, vrb.nm = colnames(mtcars), n = 2L) cleanEx() nameEx("shifts_by") ### * shifts_by flush(stderr()); flush(stdout()) ### Name: shifts_by ### Title: Shift Data (i.e., lag/lead) by Group ### Aliases: shifts_by ### ** Examples shifts_by(data = ChickWeight, vrb.nm = c("weight","Time"), grp.nm = "Chick", n = -1L) shifts_by(data = mtcars, vrb.nm = c("disp","mpg"), grp.nm = c("vs","am"), n = 1L) shifts_by(data = as.data.frame(CO2), vrb.nm = c("conc","uptake"), grp.nm = c("Type","Treatment"), n = 2L) # multiple grouping columns cleanEx() nameEx("sum_if") ### * sum_if flush(stderr()); flush(stdout()) ### Name: sum_if ### Title: Sum Conditional on Minimum Frequency of Observed Values ### Aliases: sum_if ### ** Examples sum_if(x = airquality[[1]], ov.min = .75) # proportion of observed values sum_if(x = airquality[[1]], ov.min = 116, prop = FALSE) # count of observe values sum_if(x = airquality[[1]], ov.min = 116, prop = FALSE, inclusive = FALSE) # not include ov.min value itself sum_if(x = c(TRUE, NA, FALSE, NA), ov.min = .50) # works with logical vectors as well as numeric cleanEx() nameEx("tapply2") ### * tapply2 flush(stderr()); flush(stdout()) ### Name: tapply2 ### Title: Apply a Function to a (Atomic) Vector by Group ### Aliases: tapply2 ### ** Examples # one grouping variable tapply2(mtcars$"cyl", .grp = mtcars$"vs", .fun = median, na.rm = TRUE) # two grouping variables grp_nm <- c("vs","am") # Roxygen runs the whole script if I put a c() in a [] x <- tapply2(mtcars$"cyl", .grp = mtcars[grp_nm], .fun = median, na.rm = TRUE) print(x) str(x) # compare to tapply grp_nm <- c("vs","am") # Roxygen runs the whole script if I put a c() in a [] y <- tapply(mtcars$"cyl", INDEX = mtcars[grp_nm], FUN = median, na.rm = TRUE, simplify = FALSE) print(y) str(y) # has dimnames rather than names cleanEx() nameEx("valid_test") ### * valid_test flush(stderr()); flush(stdout()) ### Name: valid_test ### Title: Test for Invalid Elements in a Vector ### Aliases: valid_test ### ** Examples valid_test(x = psych::bfi[[1]], valid = 1:6) # return TRUE valid_test(x = psych::bfi[[1]], valid = 0:5) # 6 is not present in `valid` valid_test(x = psych::bfi[[1]], valid = 1:6, na.rm = FALSE) # NA is not present in `valid` cleanEx() nameEx("valids_test") ### * valids_test flush(stderr()); flush(stdout()) ### Name: valids_test ### Title: Test for Invalid Elements in Data ### Aliases: valids_test ### ** Examples valids_test(data = psych::bfi, vrb.nm = names(psych::bfi)[1:25], valid = 1:6) # return TRUE valids_test(data = psych::bfi, vrb.nm = names(psych::bfi)[1:25], valid = 0:5) # 6 is not present in `valid` valids_test(data = psych::bfi, vrb.nm = names(psych::bfi)[1:25], valid = 1:6, na.rm = FALSE) # NA is not present in `valid` valids_test(data = ToothGrowth, vrb.nm = c("supp","dose"), valid = list("VC", "OJ", 0.5, 1.0, 2.0)) # list vector as `valid` to allow for # elements of different typeof cleanEx() nameEx("vecNA") ### * vecNA flush(stderr()); flush(stdout()) ### Name: vecNA ### Title: Frequency of Missing Values in a Vector ### Aliases: vecNA ### ** Examples vecNA(airquality[[1]]) # count of missing values vecNA(airquality[[1]], prop = TRUE) # proportion of missing values vecNA(airquality[[1]], ov = TRUE) # count of observed values vecNA(airquality[[1]], prop = TRUE, ov = TRUE) # proportion of observed values cleanEx() nameEx("wide2long") ### * wide2long flush(stderr()); flush(stdout()) ### Name: wide2long ### Title: Reshape Multiple Sets of Variables From Wide to Long ### Aliases: wide2long ### ** Examples # SINGLE GROUPING VARIABLE dat_wide <- data.frame( x_1.1 = runif(5L), x_2.1 = runif(5L), x_3.1 = runif(5L), x_4.1 = runif(5L), x_1.2 = runif(5L), x_2.2 = runif(5L), x_3.2 = runif(5L), x_4.2 = runif(5L), x_1.3 = runif(5L), x_2.3 = runif(5L), x_3.3 = runif(5L), x_4.3 = runif(5L), y_1.1 = runif(5L), y_2.1 = runif(5L), y_1.2 = runif(5L), y_2.2 = runif(5L), y_1.3 = runif(5L), y_2.3 = runif(5L)) row.names(dat_wide) <- letters[1:5] print(dat_wide) # vrb.nm.list = list of character vectors (conventional use) vrb_pat <- c("x_1","x_2","x_3","x_4","y_1","y_2") vrb_nm_list <- lapply(X = setNames(vrb_pat, nm = vrb_pat), FUN = function(pat) { str2str::pick(x = names(dat_wide), val = pat, pat = TRUE)}) # without `grp.nm` z1 <- wide2long(dat_wide, vrb.nm = vrb_nm_list) # with `grp.nm` dat_wide$"ID" <- letters[1:5] z2 <- wide2long(dat_wide, vrb.nm = vrb_nm_list, grp.nm = "ID") dat_wide$"ID" <- NULL # vrb.nm.list = character vector + guessing (advanced use) vrb_nm <- str2str::pick(x = names(dat_wide), val = "ID", not = TRUE) # without `grp.nm` z3 <- wide2long(dat_wide, vrb.nm.list = vrb_nm) # with `grp.nm` dat_wide$"ID" <- letters[1:5] z4 <- wide2long(dat_wide, vrb.nm = vrb_nm, grp.nm = "ID") dat_wide$"ID" <- NULL # comparisons head(z1); head(z3); head(z2); head(z4) all.equal(z1, z3) all.equal(z2, z4) # keeping the reshapeLong attributes z7 <- wide2long(dat_wide, vrb.nm = vrb_nm_list, keep.attr = TRUE) attributes(z7) # MULTIPLE GROUPING VARIABLES bfi2 <- psych::bfi bfi2$"person" <- unlist(lapply(X = 1:400, FUN = rep.int, times = 7)) bfi2$"day" <- rep.int(1:7, times = 400L) head(bfi2, n = 15) # vrb.nm.list = list of character vectors (conventional use) vrb_pat <- c("A","C","E","N","O") vrb_nm_list <- lapply(X = setNames(vrb_pat, nm = vrb_pat), FUN = function(pat) { str2str::pick(x = names(bfi2), val = pat, pat = TRUE)}) z5 <- wide2long(bfi2, vrb.nm.list = vrb_nm_list, grp = c("person","day"), rtn.obs.nm = "item") # vrb.nm.list = character vector + guessing (advanced use) vrb_nm <- str2str::pick(x = names(bfi2), val = c("person","day","gender","education","age"), not = TRUE) z6 <- wide2long(bfi2, vrb.nm.list = vrb_nm, grp = c("person","day"), sep = "", rtn.obs.nm = "item") # need sep = "" because no character separating # scale name and item number all.equal(z5, z6) cleanEx() nameEx("winsor") ### * winsor flush(stderr()); flush(stdout()) ### Name: winsor ### Title: Winsorize a Numeric Vector ### Aliases: winsor ### ** Examples # winsorize table(quakes$"stations") new <- winsor(quakes$"stations") table(new) # recode as NA vecNA(quakes$"stations") new <- winsor(quakes$"stations", to.na = TRUE) vecNA(new) # rtn.int = TRUE winsor(x = cars[[1]], z.min = -2, z.max = 2, rtn.int = FALSE) winsor(x = cars[[1]], z.min = -2, z.max = 2, rtn.int = TRUE) cleanEx() nameEx("winsors") ### * winsors flush(stderr()); flush(stdout()) ### Name: winsors ### Title: Winsorize Numeric Data ### Aliases: winsors ### ** Examples # winsorize lapply(X = quakes[c("mag","stations")], FUN = table) new <- winsors(quakes, vrb.nm = names(quakes)) lapply(X = new, FUN = table) # recode as NA vecNA(quakes) new <- winsors(quakes, vrb.nm = names(quakes), to.na = TRUE) vecNA(new) # rtn.int = TRUE winsors(data = cars, vrb.nm = names(cars), z.min = -2, z.max = 2, rtn.int = FALSE) winsors(data = cars, vrb.nm = names(cars), z.min = -2, z.max = 2, rtn.int = TRUE) ### *