## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize

Step 1. Read In Data:

Crime Comp_Model_PS_327 Angelo_Patri Bronx_House_Bronx_Green Jackie_Robinson Franklin_K_Lane Rockaway_Collegiate Kappa_Vi
Burglary 140 168 121 246 644 96 111
Fel_Assault 472 390 197 112 263 56 254
G.L.A. 70 64 86 48 151 18 27
GrLarceny 485 327 299 289 709 151 164
Housing 36 27 47 3 203 20 85
Misd_Sex_Crimes 779 692 353 415 1133 162 356
MisdAssault 55 28 23 28 51 12 21
Murder 9 10 2 3 16 3 5
Petit_Larceny 1177 759 635 613 1352 302 342
Rape 16 24 18 7 43 6 12
Robbery 311 303 138 150 423 46 126
Shooting_Vic. 24 26 5 13 38 14 18
ShootingInc 27 31 5 17 45 14 21
Total_Violent_Crime 1503 1286 861 855 2249 376 699
## 'data.frame':    14 obs. of  8 variables:
##  $ Crime                  : chr  "Burglary" "Fel_Assault" "G.L.A." "GrLarceny" ...
##  $ Comp_Model_PS_327      : int  140 472 70 485 36 779 55 9 1177 16 ...
##  $ Angelo_Patri           : int  168 390 64 327 27 692 28 10 759 24 ...
##  $ Bronx_House_Bronx_Green: int  121 197 86 299 47 353 23 2 635 18 ...
##  $ Jackie_Robinson        : int  246 112 48 289 3 415 28 3 613 7 ...
##  $ Franklin_K_Lane        : int  644 263 151 709 203 1133 51 16 1352 43 ...
##  $ Rockaway_Collegiate    : int  96 56 18 151 20 162 12 3 302 6 ...
##  $ Kappa_Vi               : int  111 254 27 164 85 356 21 5 342 12 ...

Step 2. Go From Wide To Long:

I used reshape2 because it seems easiest.

## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
Crime variable value
Burglary Comp_Model_PS_327 140
Fel_Assault Comp_Model_PS_327 472
G.L.A. Comp_Model_PS_327 70
GrLarceny Comp_Model_PS_327 485
Housing Comp_Model_PS_327 36
Misd_Sex_Crimes Comp_Model_PS_327 779

Step 3. Then another long format, different method, just for practice:

Crime Location Incidents
Burglary Comp_Model_PS_327 140
Fel_Assault Comp_Model_PS_327 472
G.L.A. Comp_Model_PS_327 70
GrLarceny Comp_Model_PS_327 485
Housing Comp_Model_PS_327 36
Misd_Sex_Crimes Comp_Model_PS_327 779
##             Crime Angelo_Patri Bronx_House_Bronx_Green Comp_Model_PS_327
## 1        Burglary          168                     121               140
## 2     Fel_Assault          390                     197               472
## 3          G.L.A.           64                      86                70
## 4       GrLarceny          327                     299               485
## 5         Housing           27                      47                36
## 6 Misd_Sex_Crimes          692                     353               779
##   Franklin_K_Lane Jackie_Robinson Kappa_Vi Rockaway_Collegiate
## 1             644             246      111                  96
## 2             263             112      254                  56
## 3             151              48       27                  18
## 4             709             289      164                 151
## 5             203               3       85                  20
## 6            1133             415      356                 162
Location Burglary Fel_Assault G.L.A. GrLarceny Housing Misd_Sex_Crimes MisdAssault Murder Petit_Larceny Rape Robbery Shooting_Vic. ShootingInc Total_Violent_Crime
Angelo_Patri 168 390 64 327 27 692 28 10 759 24 303 26 31 1286
Bronx_House_Bronx_Green 121 197 86 299 47 353 23 2 635 18 138 5 5 861
Comp_Model_PS_327 140 472 70 485 36 779 55 9 1177 16 311 24 27 1503
Franklin_K_Lane 644 263 151 709 203 1133 51 16 1352 43 423 38 45 2249
Jackie_Robinson 246 112 48 289 3 415 28 3 613 7 150 13 17 855
Kappa_Vi 111 254 27 164 85 356 21 5 342 12 126 18 21 699
Rockaway_Collegiate 96 56 18 151 20 162 12 3 302 6 46 14 14 376
Location Total_Violent_Crime Burglary Fel_Assault Murder Rape Robbery
Franklin_K_Lane 2249 644 263 16 43 423
Comp_Model_PS_327 1503 140 472 9 16 311
Angelo_Patri 1286 168 390 10 24 303
Bronx_House_Bronx_Green 861 121 197 2 18 138
Jackie_Robinson 855 246 112 3 7 150
Kappa_Vi 699 111 254 5 12 126
Rockaway_Collegiate 376 96 56 3 6 46

Trying bar Charts

Burglary

Robbery

Murder

Rape

Assault

Total Violent Crime

Crime Comp_Model_PS_327 Angelo_Patri Bronx_House_Bronx_Green Jackie_Robinson Franklin_K_Lane Rockaway_Collegiate Kappa_Vi
Burglary 140 168 121 246 644 96 111
Fel_Assault 472 390 197 112 263 56 254
G.L.A. 70 64 86 48 151 18 27
GrLarceny 485 327 299 289 709 151 164
Housing 36 27 47 3 203 20 85
Misd_Sex_Crimes 779 692 353 415 1133 162 356
MisdAssault 55 28 23 28 51 12 21
Murder 9 10 2 3 16 3 5
Petit_Larceny 1177 759 635 613 1352 302 342
Rape 16 24 18 7 43 6 12
Robbery 311 303 138 150 423 46 126
Shooting_Vic. 24 26 5 13 38 14 18
ShootingInc 27 31 5 17 45 14 21
Total_Violent_Crime 1503 1286 861 855 2249 376 699
## 'data.frame':    14 obs. of  8 variables:
##  $ Crime                  : chr  "Burglary" "Fel_Assault" "G.L.A." "GrLarceny" ...
##  $ Comp_Model_PS_327      : int  140 472 70 485 36 779 55 9 1177 16 ...
##  $ Angelo_Patri           : int  168 390 64 327 27 692 28 10 759 24 ...
##  $ Bronx_House_Bronx_Green: int  121 197 86 299 47 353 23 2 635 18 ...
##  $ Jackie_Robinson        : int  246 112 48 289 3 415 28 3 613 7 ...
##  $ Franklin_K_Lane        : int  644 263 151 709 203 1133 51 16 1352 43 ...
##  $ Rockaway_Collegiate    : int  96 56 18 151 20 162 12 3 302 6 ...
##  $ Kappa_Vi               : int  111 254 27 164 85 356 21 5 342 12 ...

Step 4. This works. So next, I try to rearrange the data frame to make the columns rows and rows columns. I use t() function:

Crime Burglary Fel_Assault G.L.A. GrLarceny Housing Misd_Sex_Crimes MisdAssault Murder Petit_Larceny Rape Robbery Shooting_Vic. ShootingInc Total_Violent_Crime
Comp_Model_PS_327 140 472 70 485 36 779 55 9 1177 16 311 24 27 1503
Angelo_Patri 168 390 64 327 27 692 28 10 759 24 303 26 31 1286
Bronx_House_Bronx_Green 121 197 86 299 47 353 23 2 635 18 138 5 5 861
Jackie_Robinson 246 112 48 289 3 415 28 3 613 7 150 13 17 855
Franklin_K_Lane 644 263 151 709 203 1133 51 16 1352 43 423 38 45 2249

This works. But it’s not a data frame.:

str(EdReArr) class(EdReArr) aggregate(EdReArr, X8, mean)

Step 5. So, I convert the matrix to a dataframe.:

## [1] "data.frame"
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
Crime Burglary Fel_Assault G.L.A. GrLarceny Housing Misd_Sex_Crimes MisdAssault Murder Petit_Larceny Rape Robbery Shooting_Vic. ShootingInc Total_Violent_Crime
Comp_Model_PS_327 140 472 70 485 36 779 55 9 1177 16 311 24 27 1503
Angelo_Patri 168 390 64 327 27 692 28 10 759 24 303 26 31 1286
Bronx_House_Bronx_Green 121 197 86 299 47 353 23 2 635 18 138 5 5 861
Jackie_Robinson 246 112 48 289 3 415 28 3 613 7 150 13 17 855
Franklin_K_Lane 644 263 151 709 203 1133 51 16 1352 43 423 38 45 2249

This works. But the column names become the first column.

What is the most efficient way to manage this?

How can I change the new “variable names” and eliminate the first row?