Learn R Programming

ff (version 4.0.4)

levels.ff: Getting and setting factor levels

Description

levels.ff<- sets factor levels, levels.ff gets factor levels

Usage

# S3 method for ff
levels(x)
# S3 method for ff
levels(x) <- value
 is.factor(x)
 is.ordered(x)
# S3 method for ff
is.factor(x)
# S3 method for ff
is.ordered(x)
# S3 method for default
is.factor(x)
# S3 method for default
is.ordered(x)

Arguments

x

an ff object

value

the new factor levels, if NA is an allowed level it needs to be given explicitely, nothing is excluded

Value

levels returns a character vector of levels (possibly including as.cha racter(NA)).

Details

The ff object must have an integer vmode, see .rammode. If the mode is unsigned -- see .vunsigned -- the first factor level is coded with 0L instead of 1L in order to maximize the number of codable levels. Usually the internal ff coding -- see ram2ffcode -- is invisible to the user: when subscripting from an ff factor, unsigend codings are automatically converted to R's standard factor codes starting at 1L. However, you need to be aware of the internal ff coding in two situtations. 1. If you convert an ff integer object to an ff factor object and vice versa by assigning levels and is.null(oldlevels)!=is.null(newlevels). 2. Assigning data that does not match any level usually results in NA, however, in unsigned types there is no NA and all unknown data are mapped to the first level.

See Also

ramclass, factor, virtual

Examples

Run this code
# NOT RUN {
  message("--- create an ff factor including NA as last level")
  x <- ff("a", levels=c(letters, NA), length=99)
  message('    we expect a warning because "A" is an unknown level')
  x[] <- c("a", NA,"A")
  x
  levels(x)

  message("--- create an ff ordered factor")
  x <- ff(letters, levels=letters, ramclass=c("ordered","factor"), length=260)
  x
  levels(x)

  message("    make it a non-ordered factor")
  virtual(x)$ramclass <- "factor"
  x
  rm(x); gc()

 
# }
# NOT RUN {
  message("--- create an unsigned quad factor")
  x <- ff(c("A","T","G","C"), levels=c("A","T","G","C"), vmode="quad", length=100)
  x
  message("  0:3 coding usually invisible to the user")
  unclass(x[1:4])
  message("    after removing levels, the 0:3 coding becomes visible to the user")
  message("    we expect a warning here")
  levels(x) <- NULL
  x[1:4]
  rm(x); gc()
 
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab