ds.asFactor {dsBaseClient}R Documentation

Turns a numeric vector into factor type


This function is similar to R function as.factor but it does not allow users to create factors where a categorie has less than two observations.


ds.asFactor(x = NULL, newobj = NULL, datasources = NULL)



a character, the name of numeric, integer or character vector


the name of the new vector.If this argument is set to NULL, the name of the new variable is the name of the input variable with the suffixe '_fact'.


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.


Unlike the R function 'as.factor' vectors where some levels have less than 2 observations are not allow, an empty vector (i.e. all values within it are set to NA) is then generated.


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


Gaye, A.; Burton, P.

See Also

ds.asNumeric to turn a variable into a numeric type.

ds.asList to contruct an object of type list.

ds.asCharacter to turn a variable into a character type.

ds.asMatrix to coerce an object into a matrix type.



  # load that contains the login details

  # login and assign specific variable(s)
  # (by default the assigned dataset is a dataframe named 'D')
  myvar <- list('GENDER', 'LAB_HDL')
  opals <- datashield.login(logins=logindata,assign=TRUE,variables=myvar)

  # turn the factor variable 'GENDER' into numeric and then back into factor
  ds.asNumeric(x='D$GENDER', newobj='gender_num')
  ds.asFactor(x='gender_num', newobj='gender_fact')

  # Now try to turn into a factor a numeric variable where some levels have less
  # than 2 observations (as you would expect for a continuous variable)
  # this will generate a vector missing at complte (i.e. all values within it are set to NA).
  ds.asFactor(x='D$LAB_HDL', newobj='lab.hdl.fact')
  # check the levels of the new vector

  # clear the Datashield R sessions and logout


[Package dsBaseClient version 4.1.0 ]