Learn R Programming

TeachingDemos (version 2.1)

USCrimes: US Crime Statistics

Description

This is a 3 dimensional Array of the US crime statistics downloaded from the "Uniform Crime Reporting Statistics" of the US government. It comprises the years 1960 through 2010 for all 50 states, Washington DC, and a total for the country.

Usage

data(USCrimes)

Arguments

format

The format is: num [1:52, 1:51, 1:19] 3266740 226167 1302161 1786272 15717204 ... - attr(*, "dimnames")=List of 3 ..$ State: chr [1:52] "Alabama" "Alaska" "Arizona" "Arkansas" ... ..$ : chr [1:51] "1960" "1961" "1962" "1963" ... ..$ : chr [1:19] "Population" "ViolentCrimeRate" "MurderRate" "RapeRate" ...

source

http://www.ucrdatatool.gov/

Details

The first dimension is the state, the dimnames match the variable state.name with the exception of including "District of Columbia" in the 9th position (alphabetically) and "United States-Total" in position 45 (alphabetical). The second dimension is the year, ranging from 1960 to 2010. If indexing by year, remember to put the year in quotes. The third dimension is the variable: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Examples

Run this code
data(USCrimes)
## maybe str(USCrimes)

# plot time series/sparkline for each state
if(require(maptools)) {
	data(state.vbm)
	plot(state.vbm)

	tmp.x <- state.vbm$center_x
	tmp.x <- c( tmp.x[1:8], 147, tmp.x[9:43], 83, tmp.x[44:50] )
	tmp.y <- state.vbm$center_y
	tmp.y <- c( tmp.y[1:8], 45, tmp.y[9:43], -18, tmp.y[44:50] )
	tmp.r <- range( USCrimes[,,'ViolentCrimeRate'], na.rm=TRUE)
	for(i in 1:52) {
		subplot( plot(1960:2010, USCrimes[i,,'ViolentCrimeRate'],
					ann=FALSE, bty='n', type='l', axes=FALSE),
			tmp.x[i], tmp.y[i], size=c(0.2,0.2) )
	}
}


## Gapminder style animation over time
if( interactive() ) {
x.r <- range( USCrimes[-c(9,45),,'Population'], na.rm=TRUE )
y.r <- range( USCrimes[-c(9,45),,'PropertyCrimeRate'], na.rm=TRUE )

tmpfun <- function(Year=1960, ... ) {
	y <- as.character(Year)
	plot( USCrimes[-c(9,45),y,'Population'],
		 USCrimes[-c(9,45),y,'PropertyCrimeRate'],
		type='n', xlab='log Population',
		ylab='Property Crime Rate',
		main=y, xlim=x.r, ylim=y.r, log='x' )
	text( USCrimes[-c(9,45),y,'Population'],
		 USCrimes[-c(9,45),y,'PropertyCrimeRate'],
		state.abb, ... )
}

tmp.list <- list( Year=list('animate', from=1960, to=2010, delay=250) )

tmpcol <- c('blue','darkgreen','red','purple')[state.region]
tkexamp( tmpfun(col=tmpcol), tmp.list )
}

Run the code above in your browser using DataLab