# Generate systematic and random data

## Empty objects

N <- 5
numeric(N)
[1] 0 0 0 0 0
matrix(numeric(N))
     [,1]
[1,]    0
[2,]    0
[3,]    0
[4,]    0
[5,]    0
character(N)
[1] "" "" "" "" ""
vector(mode="list", length=N)
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

[[5]]
NULL

## Systematic numerical sequences

### Ordered sequences

20:26
[1] 20 21 22 23 24 25 26
26:20
[1] 26 25 24 23 22 21 20
-4:2
[1] -4 -3 -2 -1  0  1  2
-(4:2)
[1] -4 -3 -2
seq(from=2, to=12, by=2)
[1]  2  4  6  8 10 12
seq(from=2, to=11, by=2)
[1]  2  4  6  8 10
seq(from=0, to=-1, length.out=5)
[1]  0.00 -0.25 -0.50 -0.75 -1.00
age <- c(18, 20, 30, 24, 23, 21)
seq(along=age)
[1] 1 2 3 4 5 6
vec <- numeric(0)
length(vec)
[1] 0
1:length(vec)
[1] 1 0
seq(along=vec)
integer(0)

### Repeated sequences

rep(1:3, times=5)
 [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
rep(c("A", "B", "C"), times=c(2, 3, 4))
[1] "A" "A" "B" "B" "B" "C" "C" "C" "C"
rep(age, each=2)
 [1] 18 18 20 20 30 30 24 24 23 23 21 21

## Random data

Strictly, the data is pseudorandom. There are several options for the random number generator, see RNGkind(). Use set.seed() to set the state of the RNG. This allows to replicate the following sequence of numbers. Copy .Random.seed into your own object to save the current state of the RNG. Donâ€™t modify .Random.seed.

### Sample from an urn

set.seed(123)
sample(1:6, size=20, replace=TRUE)
 [1] 3 6 3 2 2 6 3 5 4 6 6 1 2 3 5 3 3 1 4 1
sample(c("rot", "gruen", "blau"), size=8, replace=TRUE)
[1] "rot"   "rot"   "blau"  "gruen" "blau"  "gruen" "rot"   "gruen"
x <- c(2, 4, 6, 8)
sample(x[(x %% 4) == 0])
[1] 4 8
sample(x[(x %% 8) == 0])
[1] 6 1 3 5 4 8 2 7

### Data from random variables with different distributions

runif(5, min=1, max=6)
[1] 2.844227 1.762224 1.694030 2.165170 3.329812
rbinom(20, size=5, prob=0.3)
 [1] 1 3 0 1 2 0 2 1 0 2 3 1 2 0 1 1 2 1 2 2
rchisq(4, df=7)
[1] 9.183538 8.622633 8.072239 5.787448
rnorm(6, mean=100, sd=15)
[1] 104.29823  81.69232 106.51826 112.00265  97.54104 118.64378
rt(5, df=5, ncp=1)
[1]  0.07261917  1.83920988  1.59345974  0.11567864 -0.56678656
rf(5, df1=2, df2=10)
[1] 2.9532047 0.4913562 7.5093655 0.6704753 0.4034911

See ?Distributions for more distribution types. Even more information can be found in CRAN task view Probability Distributions.