midp2brk {lazy.tools}R Documentation

Define Break Points from Midpoints

Description

Define Break Points from Midpoints

Usage

midp2brk(midpoints, min = NULL, max = NULL, margin = NULL)

Arguments

midpoints

a vector of midpoints.

min

value of the lowest break.

max

value of the highest break.

margin

value to be subtracted/added to the lowest and highest midpoints.
default margin = ( midpoints[2]-midpoints[1] )/2

Details

Break values from midpoint values:
brk[1]=midp[1] - margin
brk[2]=midp[1] + (midp[2]-midp[1])/2
brk[3]=midp[2] + (midp[3]-midp[2])/2
brk[length(midpoints)+1]=midp[length(midpoints)] + margin
where margin=(midp[2]-midp[1])/2.
In general, the break point is located at the center of two midpoints:
brk[k]=0.5*(midp[k-1]+midp[k])

Midpoint values from break values:
midp[1]=0.5*(brk[1]+brk[2]) or midp[1]=min
midp[2]=0.5*(brk[2]+brk[3])
midp[3]=2*brk[3]-midp[2]
brk[length(brk)-1]=2*brk[length(brk)-1]-midp[length(brk)-2]

Note that midp[1] is treated separately and it is assumed that the break point is located at the center of two midpoints.

Try below to see how the obs on the border are classified.
xx <- c(1,2,3,4,5,6,7)
brks <- c(0,2,4,6,8)
cc1 <- cut(xx,brks)
cc2 <- cut(xx,brks, right=FALSE)
Print(xx,as.character(cc1),as.character(cc2))

Value

A vector of break points of length length(midpoints)+1.

Examples

midp <- seq(0,5,length.out=11)
brk <- midp2brk( midp, margin=1 )
Print(midp,brk)

midp <- seq(0,5,length.out=11)
brk <- midp2brk( midp, margin=1 )
Print(midp,brk)

set.seed(1701)
x <- rnorm(100)+2.5

# histogram by native hist
resh <- hist( x, breaks=brk )

# histogram by native barplot
# freq <- resh$counts
# names(freq) <- midp
# barplot( freq, space=0 )
barplot( resh$count,names.arg=midp, space=0, xlab="X", col="white"
        , sub="top and bottom bars contain outliers." )

# Below causes error because 'brk' does not span the range of 'x'.
# brk <- midp2brk( midp )
# resh <- hist( x, breaks=brk )


[Package lazy.tools version 0.1.4 ]