R/pairCount.R

Defines functions pairCount

Documented in pairCount

#' Calculates the frequency each pair of observations within a dataset.
#' This function is needed by weightedPlot, which uses paired observation frequency
#' to generate graph points whose area is proportional to frequency.
#'
#' @param x A vector
#' @param y A vector
#' @param roundX Number of digits for rounding x vector
#' @param roundY Number of digits for rounding y vector
#' @return Returns a vector of pair counts
#' @seealso weightedPlot
#' @seealso round


pairCount <- function(x, y, roundX=NULL, roundY=NULL){
	# Make sure the lengths of the input vectors are equal
	if(length(x) != length(y)){
		stop("Vectors are of unequal length")
	}

	# Create a holder for the frequency data
	pair_count <- NA

	# If rounding digits are specified, round the vectors appropriately
	if(!is.null(roundX)){
		x <- round(x, roundX)
	}
	if(!is.null(roundY)){
		y <- round(y, roundY)
	}
	message("Frequencies of y")
	print(table(y))

	# Loop through the vectors and count frequencies
	for(i in 1:length(x)){
		pair_count[i] <- sum(x == x[i] & y == y[i], na.rm=TRUE)
	}

	# Turn "0" observations into "NA" so they're ignored
	pair_count <- ifelse(pair_count == 0, NA, pair_count)

	# Return the vector of frequencies
	return(pair_count)
}
michaelpmcdonald/rgo documentation built on May 22, 2019, 9:52 p.m.