Determine if a vector is ordered

This little bit of code tells you if a vector x is ordered: 1 = ascending, 2 = descending, 0 = not ordered.

ordered.status <- function (x) {
	if (x[1] > x[2]) {
		ord <- order(x, decreasing = T)
		ordx <- x[ord]
		return(ifelse(identical(x, ordx), 1, 0))
		} else {
			ord <- order(x)
			ordx <- x[ord]
			return(ifelse(identical(x, ordx), 2, 0))
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s