uncount: Generate raw data from count data

Description Usage Arguments Value Examples

View source: R/uncount.R

Description

Generate raw data from a count table or a count data frame. A count table is a contingency table for only one factor. A count data frame is a data frame consisting of two columns: the first column contains all the different factor levels, and the second column, the corresponding counts.

Usage

1

Arguments

x

Table (i.e. the output of table function) with only one dimension or a data frame with two columns, the second one being numeric.

Value

A vector whose length is equal to the sum of counts. It is a factor if x is a table. Otherwise if x is a data frame, the class of the return value is the same as the class of the first column of x.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
set.seed(12345)
x1 <- sort(rgeom(100, prob = 0.5))
y <- table(x1)
y
x2 <- uncount(y)
x2 <- as.integer(as.character(x2))
# More efficient, but harder to remember:
# x2 <- as.integer(levels(x2)[as.integer(x2)])
identical(x1, x2)

require(MASS)
x <- data.frame(label = c(1, 2, 3, 4, 6, 9),
                count = c(4, 7, 4, 1, 1, 1))
x <- uncount(x)
res <- fitdistr(x, "Poisson")
res

chgigot/cgmisc documentation built on May 14, 2019, 8:17 a.m.