##
## 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:
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
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 |
Total Violent Crime
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"
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?