File: fac2seg.R

package info (click to toggle)
r-cran-pls 2.8-5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,056 kB
  • sloc: sh: 13; makefile: 2
file content (17 lines) | stat: -rw-r--r-- 459 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#' Factor to Segments
#'
#' @param fac A factor where each level represents a segment
#'
#' @return A list of vectors, each vector contains the indices of the elements
#' of the corresponding segment
#' @export
#'
#' @examples
#' fac <- factor(c("a", "b", "a", "b", "c", "c"))
#' fac2seg(fac)
fac2seg <- function(fac){
  if(!is.factor(fac))
    stop("Input must be a factor")
  lapply(1:length(levels(fac)),
         function(i) which(fac==levels(fac)[i]))
}