compress_freqdist {lazy.tools} | R Documentation |
Compress Frequency Distribution by Aggregating Duplicate Entries
compress_freqdist(x, freq, fill = 0, domain = NULL)
x |
Vector of values (domain of a scalar variable, numeric or string) |
freq |
Vector of frecuencies:
|
fill |
= 1 to fill the domain |
domain |
Domain of |
The purpose of this function is to remove the duplicate values of x
and adjusting the associated frequency count in freq
.
The core part of this function is:
fnew <- xtabs( freq ~ x ) xnew <- names(fnew) res <- data.frame(x=xnew, freq=fnew)
and if domain
and fill
are given, the result will be
coalesced. (coalescing)
Using the frequency table below
x freq 1 1 2 2 2 1 3 1
compress_freqdist( x, freq, fill=1, domain=0:4)
returns
x freq 0 0 1 1 2 3 3 1 4 0
A data frame consisting of
x
Vector of unique values
freq
Vector of frequencies
x <- c(1,2,4,5,6, 2,4,6, 9 )
f <- c(1,2,2,1,2, 1,2,1, 1 )
( res <- compress_freqdist( x, f ) )
( res <- compress_freqdist( x, f, fill=1, domain=0:10 ) )
xc <- letters[c(1,2,4,5,6, 2,4,6, 9 )]
f <- c(1,2,2,1,2, 1,2,1, 1 )
( res <- compress_freqdist( xc, f ) )
( res <- compress_freqdist( xc, f, fill=1, domain=letters[0:10] ) )