Generate Factor Levels
Generate factors by specifying the pattern of their levels.
gl(n, k, length = n*k, labels = seq_len(n), ordered = FALSE)
- an integer giving the number of levels.
- an integer giving the number of replications.
- an integer giving the length of the result.
- an optional vector of labels for the resulting factor levels.
- a logical indicating whether the result should be ordered or not.
The result has levels from
nwith each value replicated in groups of length
kout to a total length of
glis modelled on the GLIM function of the same name.
## First control, then treatment: gl(2, 8, labels = c("Control", "Treat")) ## 20 alternating 1s and 2s gl(2, 1, 20) ## alternating pairs of 1s and 2s gl(2, 2, 20)
## Generate a vector of (10) normal random variables with a mean of 0, (10) uniformed variables with mean of 0 and (10) normal ranfom variables with a mean of 1 x <- c(rnorm(10), runif(10), rnorm(10, 1)) ## Generate a factor of (3) levels that repeat (10) times using gl() ## We select (3) for factor levels because x defined (3) different sets (that will be represented as the levels) of random variables in its definition g <- gl(3, 10) ## Use split() to output the values of the three sections (levels) of x as a list using gl()'s factor split(x, g)