R: Getting and setting factor levels

levels.ff {ff} | R Documentation

## Getting and setting factor levels

### Description

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

### Usage

    ## S3 method for class 'ff'
    levels(x)
    ## S3 replacement method for class 'ff'
    levels(x) <- value
     is.factor(x)
     is.ordered(x)
    ## S3 method for class 'ff'
    is.factor(x)
    ## S3 method for class 'ff'
    is.ordered(x)
    ## Default S3 method:
    is.factor(x)
    ## Default S3 method:
    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

### 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.

### Value

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

### Note

When levels as assigned to an ff object that formerly had not levels, we assign automatically `ramclass` == "factor". If you want to change to an ordered factor, use `virtual$ramclass <- c("ordered", "factor")`

### Author(s)

Jens Oehlschlägel

### See Also

`ramclass`, `factor`, `virtual`

### Examples

      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()

    ## End(Not run)

[Package ff version 2.2-13 Index]