factorChange: factorChange

Description Usage Arguments Value Examples

View source: R/factorChange.R

Description

factorChange takes as input a data.frame object (dfData), a vector of column names (dfColNameVec), a list of vectors of ordered levels (levelList) one for each column in dfColNameVec and modifies the nomenclature and order of the levels of column(s) in the data.frame object dfData. For this function, first the user needs to check the levels of each column in colNameVec and then decide the new values that are to be used and choose an ordering scheme for the levels of each column. E.g. if the original levels are ("A","B","C"), and the user wants to order them as ("C","A","B"), then the user needs to specify orderList = c(3,1,2).

Usage

1
2
3
4
5
6
7
8
9
factorChange(
  dfData,
  dfColNameVec,
  levelList,
  orderList,
  outputFormat = "beside",
  suffix = "M",
  envir = .GlobalEnv
)

Arguments

dfData

a data.frame object to be used. Note that this is not a character string but the data.frame object itself. The parameter passed to the function should be without quotes (" ").

dfColNameVec

a string or a vector of class character indicating the name(s) of the column(s) to be handled. All these columns belong to the class factor.

levelList

an object of class list containing character vectors of the ordered levels for the columns in dfColNameVec. If a list with only one vector is provided, that vector is used for the levels reference of all columns in dfColNameVec. It is important that an object of class list is passed, even if it contains only one vector. Passing a single vector of class character gives incorrect results.

orderList

a list object containing the order of the levels of the columns in dfColNameVec. If nothing is specified (i.e. missing orderList) then the default ordering (1:n) is used.

outputFormat

a character string (default: "beside") indicating whether to insert the new columns right after the respective columns (outputFormat: "beside") or append them at the end of the data.frame object dfData.

suffix

a string (default: "M") of class character indicating the suffix to be added to the original column name to obtain the column name for the new column with updated factors.

envir

a variable (default: .GlobalEnv) indicating the environment where the output data.frame object should be saved.

Value

factorChange returns the modified data.frame object with updated entries for the columns in dfColNameVec. The function changes the levels of these columns to the values specified in the list levelList.

Examples

1
2
3
factorChange(dfData,c("col1","col2"),c("low","medium", "high"))
factorChange(iris,"Species",list(c("species-A","species-B","species-C")), list(c(1,3,2)))
factorChange(iris,"Species",list(c("species-A","species-B","species-C")),list(c(3,1,2)))

lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.