Example R programs and commands
2. Grouping into integer bins; histograms; samples
# All lines preceded by the "#" character are my comments.
# All other left-justified lines are my input.
# All other indented lines are the R program output.
# Input data
z <- c(1.1, 2.0, 1.5, 2.1, 1.9, 1.8, 3.5, 3.6, 3.7)
# Breakpoints for integer bins [n,n+1)
ibpts <- seq(from=0,to=4,by=1); ibpts
[1] 0 1 2 3 4
# Plot the histogram of the integer binned values
hist(z, breaks=ibpts)
<>
# Another way to group values into integer bins [n,n+1)
as.integer(z)
[1] 1 2 1 2 1 1 3 3 3
# Plot the cumulative relative frequency graph of the integer binned values,
# also known as the empirical cumulative distribution function or ecdf:
plot(ecdf(as.integer(z)))
<>
# Choose integer bin midpoints as representatives:
as.integer(z)+0.5
[1] 1.5 2.5 1.5 2.5 1.5 1.5 3.5 3.5 3.5
# Compute median and mean from binned data with midpoint representatives:
mean(as.integer(z)+0.5)
[1] 2.388889
median(as.integer(z)+0.5)
[1] 2.5
# Choose some samples from a list:
sample(z,3)
[1] 1.8 1.1 1.9
# Repeat to get another 3 samples:
sample(z,3)
[1] 1.8 1.5 2.0
# Sample with replacement, so some value may be chosen more than once:
sample(z,3,replace=TRUE)
[1] 2.0 1.1 1.1