Learn R Programming

prob (version 1.0-1)

countrep: Count Repetitions

Description

Counts the number of repetitions of vals in a given vector x.

Usage

countrep(x, …)

# S3 method for default countrep(x, vals = unique(x), nrep = 2, …)

# S3 method for data.frame countrep(x, …)

Arguments

x

an object in which repeats should be counted.

vals

values that may be repeated.

nrep

exact number of repeats desired, defaults to pairs.

further arguments to be passed to or from other methods.

Value

If x is a vector, then the value is an integer. If x is a data frame then the value is a vector, with entries the corresponding value for the respective rows of x

Details

This is a generic function, with methods supplied for data frames and vectors. The default behavior counts the number of pairs of elements of x. One can find the number of triples, etc., by changing the nrep argument. If there are specific values for which one is looking for repeats, these can be specified with the vals argument. Note that the function only checks for exactly nrep instances, so two pairs of a specific element would be counted as 0 pairs and 1 quadruple. See the examples.

The data frame method uses apply to apply countrep.default to each row of the data frame.

See Also

isrep

Examples

Run this code
# NOT RUN {
x <- c(3,3,2,2,3,3,4,4)
countrep(x)  # one pair each of 2s and 4s
countrep(x, nrep = 4) 
countrep(x, vals = 4) # one pair of 4s
# }

Run the code above in your browser using DataLab