expand_freqdist {lazy.tools} | R Documentation |
Expand Frequency Distribution to Raw Data
expand_freqdist(fd, weight = NULL)
fd |
tables, matrices or data.frames containing frequency
distribution (midpoints,freq), or |
weight |
Frequency count of each row of fd. |
In univariate case, this function returns
unlist( mapply( function(x,y) rep(x,y), fd[,1], fd[,2] ) )
or
rep( fd[,1], fd[,2] )
A vector of length( sum(weight) ) or
matrix or data frame with nrow=sum(weight).
seed <- 1701
set.seed(seed)
# univariate
tab1 <- table( sample(letters[1:5], 20, replace=1) )
res1 <- expand_freqdist( tab1 )
tab11 <- table(res1)
Print(tab1,res1, tab11)
# convert a table object to a freqdist format
res11 <- expand_freqdist( table2freqdist(tab1) )
Print(res11)
# multivariate
tab2 <- table( x=sample(letters[1:5], 20, replace=1)
, sample(6:8, 20, replace=1) )
res2 <- expand_freqdist( tab2 )
tab22 <- table(res2)
Print(tab2,res2,tab22)
# convert a table object to a freqdist format
res22 <- expand_freqdist( table2freqdist(tab2) )
Print(res22)
# multivariate
temp <- c(
1, -3.0, 1, 0, 2, -2.6, 2, 0, 3, -2.3, 3, 0, 4, -1.9, 4, 0, 5, -1.5, 5, 1
, 6, -1.1, 6, 1, 7, -0.8, 7, 2, 8, -0.4, 8, 1, 9, 0.0, 9, 7, 10, 0.4, 8, 7
, 11, 0.8, 7, 5, 12, 1.1, 6, 6, 13, 1.5, 5, 4, 14, 1.9, 4, 4, 15, 2.3, 3, 2
, 16, 2.6, 2, 2, 17, 3.0, 1, 1
)
data_freq <- as.data.frame(matrix(temp,,4, byrow=1))
colnames(data_freq) <- c("obs","x","f","r")
res <- expand_freqdist( data_freq, weight=data_freq$f)
# equivalent to rep
Print( rep( c(1:4),c(1:4) ), expand_freqdist( c(1:4),c(1:4) ) )