Permute the levels of a factor
Apply an arbitrary permutation to the ordering of levels within a factor
permuteLevels(x,perm,ordered = is.ordered(x),invert=FALSE)
- The factor to be permuted
- A vector specifying the permutation
- Should the output be an ordered factor?
- Use the inverse of
permto specify the permutation
This is a convenience function used to shuffle the order in which the levels of a factor are specified. It is similar in spirit to the
relevel function, but more general. The
relevel function only changes the first level of the factor, whereas
permuteLevels can apply an arbitrary permutation. This can be useful for plotting data, because some plotting functions will display the factor levels in the same order that they appear within the factor.
perm argument is a vector of the same length as
levels(x), such that
perm[k] is an integer that indicates which of the old levels should be moved to position k. However, if
invert=TRUE, the inverse permutation is applied: that is,
perm[k] is an integer specifying where to move the k-th level of the original factor. See the Examples for more details.
Returns a factor with identical values, but with the ordering of the factor levels shuffled.
This package is under development, and has been released only due to teaching constraints. Until this notice disappears from the help files, you should assume that everything in the package is subject to change. Backwards compatibility is NOT guaranteed. Functions may be deleted in future versions and new syntax may be inconsistent with earlier versions. For the moment at least, this package should be treated with extreme caution.
#### Example # original factor specifies the levels in order: a,b,c,d,e,f x <- factor( c(1,4,2,2,3,3,5,5,6,6), labels=letters[1:6] ) print(x) # apply permutation (5 3 2 1 4 6)... i.e., move 5th factor level (e) # into position 1, move 3rd factor level (c) into position 2, etc permuteLevels(x,perm = c(5,3,2,1,4,6)) # apply the inverse of permutation (5 3 2 1 4 6)... i.e., move 1st # level (a) into position 5, move 2nd level (b) into position 3, etc permuteLevels(x,perm = c(5,3,2,1,4,6),invert=TRUE)