ds.recodeLevels {dsBaseClient}R Documentation

Recodes the levels of a factor vector

Description

The function replaces the levels of a factor by the specified ones.

Usage

ds.recodeLevels(x = NULL, newCategories = NULL, newobj = NULL,
  datasources = NULL)

Arguments

x,

a character, the name of a factor variable.

newCategories,

a character vector, the new levels. Its length MUST be equal or greater to the current number of levels.

newobj,

a character, the name of the new factor vector. If no name is specified for the new variable it is named after the input variable with a suffixe '_new'.

datasources

a list of opal object(s) obtained after login in to opal servers; these objects hold also the data assign to R, as dataframe, from opal datasources. By default an internal function looks for 'opal' objects in the environment and sets this parameter.

Details

It uses the R function 'levels()' on the client side to alter the current levels. It can for example be used to merge two classes into one, to add a level(s) to a vector or to rename (i.e. re-label) the levels of a vector.

Value

nothing is returned to the client, the new object is stored on the server side.

Author(s)

Gaye, A.

Examples

{

  # load that contains the login details
  data(logindata)

  # login and assign all the variables
  opals <- datashield.login(logins=logindata,assign=TRUE)

  # let s first check the levels in the categorical variable 'PM_BMI_CATEGORICAL'
  ds.levels(x='D$PM_BMI_CATEGORICAL')

  # Example1: merge the levels '2' and '3' to obtain only two levels (i.e. '1' and '2')
  # this is the same as recoding level '3' as '2' whilst keeping the same labels for the other two levels.
  ds.recodeLevels(x='D$PM_BMI_CATEGORICAL', newCategories=c('1','2','2'), newobj='BMI_CAT_NEW1')
  ds.levels(x='BMI_CAT_NEW1')

  # Example2: add a 4th and empty level to categorical bmi to create a new variable
  # we know the current categories are '1', '2' and '3' so we add '4'
  ds.recodeLevels(x='D$PM_BMI_CATEGORICAL', newCategories=c('1','2','3','4'), newobj='BMI_CAT_NEW2')
  ds.levels(x='BMI_CAT_NEW2')

  # Example3: re-label the levels of the categorical bmi "low", "mid" and "high"
  ds.recodeLevels(x='D$PM_BMI_CATEGORICAL', newCategories=c('low','mid','high'), newobj='BMI_CAT_NEW3')
  ds.levels(x='BMI_CAT_NEW3')

  # clear the Datashield R sessions and logout
  datashield.logout(opals)

}

[Package dsBaseClient version 4.1.0 ]