recode2other: Recode Unique Values in a Character Vector to 0ther (or NA)

Description Usage Arguments Details Value See Also Examples

View source: R/quest_functions.R

Description

recode2other recodes multiple unique values in a character vector to the same new value (e.g., "other", NA_character_). It's primary use is to recode based on the minimum frequency of the unique values so that low frequency values can be combined into the same category; however, it also allows for recoding particular unique values given by the user (see details). This function is a wrapper for car::recode, which can handle general recoding of character vectors.

Usage

1
2
3
4
5
6
7
8
recode2other(
  x,
  freq.min,
  prop = FALSE,
  inclusive = TRUE,
  other.nm = "other",
  extra.nm = NULL
)

Arguments

x

character vector. If not a character vector, it will be coarced to one via as.character.

freq.min

numeric vector of length 1 specifying the minimum frequency of a unique value to keep it unchanged and consequentially recode any unique values with frequencues less than (or equal to) it.

prop

logical vector of length 1 specifying if freq.min provides the frequency as a count (FALSE) or proportion (TRUE).

inclusive

logical vector of length 1 specifying whether the frequency of a unique value exactly equal to freq.min should be kept unchanged (and not recoded to other.nm).

other.nm

character vector of length 1 specifying what value the other unique values should be recoded to. This can be NA_character_ resulting in recoding to a missing value.

extra.nm

character vector specifying extra unique values that should be recoded to other.nm that are not included based on the minimum frequency from the combination of freq.min, prop, inclusive. The default is NULL, meaning no extra unique values are recoded.

Details

The extra.nm argument allows for recode2other to be used as simpler function that just recodes particular unique values to the same new value (although arguably this is easier to do using car::recode directly). To do so set freq.min = 0 and provide the unique values to extra.nm. Note, that the current version of this function does not allow for NA_character_ to be included in extra.nm as it will end up treating it as "NA" (see examples).

Value

character vector of the same length as x with unique values with frequency less than freq.nm recoded to other.nm as well as any unique values in extra.nm. While the current version of the function allows for recoding *to* NA values via other.nm, it does not allow for recoding *from* NA values via extra.nm (see examples).

See Also

recode ifelse

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 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

quest documentation built on Sept. 10, 2021, 5:07 p.m.

Related to recode2other in quest...