levels.ff.rdlevels.ff<- sets factor levels, levels.ff gets factor levels
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.
levels returns a character vector of levels (possibly including as.cha racter(NA)).
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")
message("--- create an ff factor including NA as last level")
#> --- 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')
#> we expect a warning because "A" is an unknown level
x[] <- c("a", NA,"A")
#> Warning: unknown factor values mapped to NA
x
#> ff (open) integer length=99 (99) levels: a b c d e f g h i j k l m n o p q r s t u v w x y z <NA>
#> [1] [2] [3] [4] [5] [6] [7] [8] [92] [93] [94] [95] [96] [97] [98]
#> a NA NA a NA NA a NA : NA NA a NA NA a NA
#> [99]
#> NA
levels(x)
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z" NA
message("--- create an ff ordered factor")
#> --- create an ff ordered factor
x <- ff(letters, levels=letters, ramclass=c("ordered","factor"), length=260)
x
#> ff (open) integer length=260 (260) levels: a < b < c < d < e < f < g < h < i < j < k < l < m < n < o < p < q < r < s < t < u < v < w < x < y < z
#> [1] [2] [3] [4] [5] [6] [7] [8] [253] [254] [255] [256]
#> a b c d e f g h : s t u v
#> [257] [258] [259] [260]
#> w x y z
levels(x)
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
message(" make it a non-ordered factor")
#> make it a non-ordered factor
virtual(x)$ramclass <- "factor"
x
#> ff (open) integer length=260 (260) levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
#> [1] [2] [3] [4] [5] [6] [7] [8] [253] [254] [255] [256]
#> a b c d e f g h : s t u v
#> [257] [258] [259] [260]
#> w x y z
rm(x); gc()
#> used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 1161015 62.1 1994352 106.6 1994352 106.6
#> Vcells 2163286 16.6 8790397 67.1 8790397 67.1
if (FALSE) { # \dontrun{
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()
} # }