Reading and writing in one operation (high-level)
swap.rdThe generic swap combines x[i] and x[i] <- value in a single operation.
Usage
swap(x, value, ...)
# S3 method for class 'ff'
swap(x, value, i, add = FALSE, pack = FALSE, ...)
# S3 method for class 'ff_array'
swap(x, value, ..., bydim = NULL, drop = getOption("ffdrop"), add = FALSE, pack = FALSE)
# Default S3 method
swap(x, value, ..., add = FALSE)Arguments
- x
a ff or ram object
- value
the new values to write, possibly recycled, see
[.ff- i
index information, see
[.ff- ...
missing OR up to length(dim(x)) index expressions OR (ff only)
hiobjects- drop
logical scalar indicating whether array dimensions shall be dropped
- bydim
how to interpret vector to array data, see
[.ff- add
TRUE if the values should rather increment than overwrite at the target positions, see
readwrite.ff- pack
FALSE to prevent rle-packing in hybrid index preprocessing, see
as.hi
Details
y <- swap(x, value, i, add=FALSE, ...)
is a shorter and more efficient version of
y <- x[i, add=FALSE, ...]
x[i, add=FALSE, ...] <- value
and
y <- swap(x, value, i, add=TRUE, ...)
is a shorter and more efficient version of
y <- x[i, add=TRUE, ...]
y <- y + value
x[i, add=FALSE, ...] <- y
Value
Values at the target positions.
More precisely swap(x, value, i, add=FALSE) returns the old values at the position i while swap(x, value, i, add=TRUE) returns the incremented values of x.
Note
Note that swap.default changes the object in its parent frame and thus violates R's usual functional programming logic.
When using add=TRUE, duplicated index positions should be avoided, because ff and ram objects behave differently:
swap.ff(x, 1, c(3,3), add=TRUE)
# will increment x at position 3 TWICE by 1, while
swap.default(x, 1, c(3,3), add=TRUE)
# will increment x at position 3 just ONCE by 1
Examples
x <- ff("a", levels=letters, length=52)
y <- swap(x, "b", sample(length(x), 26))
x
#> ff (open) integer length=52 (52) 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] [45] [46] [47] [48] [49] [50] [51]
#> a b b a a a a a : a b a b a a b
#> [52]
#> b
y
#> [1] a a a a a a a a a a a a a a a a a a a a a a a a a a
#> 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
rm(x,y); gc()
#> used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 1271406 68.0 2239654 119.7 2239654 119.7
#> Vcells 2345082 17.9 8388608 64.0 8387257 64.0