Download the cv2010on.csv file from Moodle; save it in your computer; and upload it into the data folder of your RStudio. Revise the read.csv code line so that the code matches both the name and address of the data file. The data contains civil cases filed at the federal district courts in six New England States in 2011 and on. The row represents civil cases, and the column their characteristics. DEF stands for defendants; PLT plaintiffs; and nature_of_suit type of lawsuits. Change civilCases to civilCases.

Q1 How many cases are there?

There are 36643 obs cases

# Load dplyr package
library(dplyr) #for use of dplyr functions such as glimpse(), mutate(), and filter()
library(ggplot2) #for use of ggplot2 functions such ggplot()

# Import data
civilCases <- read.csv("data/cv2010on.csv") 

# Convert data to tbl_df
civilCases <- tbl_df(civilCases)
str(civilCases)
## Classes 'tbl_df', 'tbl' and 'data.frame':    36643 obs. of  6 variables:
##  $ DISTRICT      : Factor w/ 6 levels "CT","MA","ME",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ PLT           : Factor w/ 19900 levels "-8",":WALKER EL: VENUS-ANTOINETTE",..: 6393 3300 5130 19442 7175 3482 6269 4384 12436 13162 ...
##  $ DEF           : Factor w/ 19496 levels "-8","'47 BRAND, LLC",..: 8018 11968 5576 10445 5251 14988 7759 1510 8210 13180 ...
##  $ FILEYEAR      : int  2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 ...
##  $ NOS           : int  445 385 442 440 440 190 440 442 190 110 ...
##  $ nature_of_suit: Factor w/ 44 levels "ADMINISTRATIVE PROCEDURE ACT/REVIEW OR APPEAL OF AGENCY\nDECISION",..: 7 37 9 28 28 29 28 9 29 19 ...

Q2 List all values in District variable.

Revise the level code below so that R returns all levels (values) in the id variable.

There are 6 variables in district variable

Q3 How many HEALTH CARE / PHARM cases were filed in New Hampshire?

Revise the table code below so that R returns the answer for the question.

There are 445 HEALTH CARE / PHARM cases were filed in New Hampshire

levels(civilCases$DISTRICT)
## [1] "CT" "MA" "ME" "NH" "RI" "VT"

tab <- table(civilCases$DISTRICT, civilCases$nature_of_suit)

tab
##     
##      ADMINISTRATIVE PROCEDURE ACT/REVIEW OR APPEAL OF AGENCY\nDECISION
##   CT                                                                21
##   MA                                                                84
##   ME                                                                18
##   NH                                                                11
##   RI                                                                11
##   VT                                                                 6
##     
##      ANTITRUST ARBITRATION ASSAULT, LIBEL, AND SLANDER BANKS AND BANKING
##   CT        38          37                          51                15
##   MA       105          71                         117                35
##   ME         3           7                          28                 2
##   NH         2           6                          15                 9
##   RI        27          11                          15                 9
##   VT         9           0                          14                 4
##     
##      CIVIL RIGHTS ACCOMMODATIONS CIVIL RIGHTS ADA EMPLOYMENT
##   CT                          59                         312
##   MA                         132                         201
##   ME                          13                         114
##   NH                           9                          77
##   RI                          15                          66
##   VT                           6                          17
##     
##      CIVIL RIGHTS ADA OTHER CIVIL RIGHTS JOBS CONSUMER CREDIT
##   CT                     75              1389             943
##   MA                    388               843             745
##   ME                     38               300              65
##   NH                     42               200             110
##   RI                     49               234             122
##   VT                     15                60              15
##     
##      CONTRACT FRANCHISE CONTRACT PRODUCT LIABILITY COPYRIGHT
##   CT                 60                         12       273
##   MA                 36                         53       338
##   ME                  8                          5        16
##   NH                  3                          9        29
##   RI                  4                          6        22
##   VT                  0                          6         7
##     
##      FAIR LABOR STANDARDS ACT FALSE CLAIMS ACT
##   CT                      365               47
##   MA                      378              111
##   ME                       50               20
##   NH                       29                4
##   RI                      114               16
##   VT                       18               10
##     
##      FAMILY AND MEDICAL LEAVE ACT FOOD AND DRUG ACTS HEALTH CARE / PHARM
##   CT                          168                  0                  53
##   MA                          112                  0                4493
##   ME                           20                  0                  72
##   NH                           15                  0                 445
##   RI                           29                  1                  67
##   VT                            3                  0                  15
##     
##      INSURANCE INTERSTATE COMMERCE LABOR/MANAGEMENT RELATIONS ACT
##   CT       645                   8                             26
##   MA       625                  24                            127
##   ME        98                   7                              7
##   NH        95                   3                              6
##   RI       210                   3                             19
##   VT        68                   5                              3
##     
##      LABOR/MANAGEMENT REPORT & DISCLOSURE MEDICAL MALPRACTICE
##   CT                                    0                  70
##   MA                                    1                 122
##   ME                                    0                  39
##   NH                                    0                  33
##   RI                                    0                  30
##   VT                                    0                  39
##     
##      MOTOR VEHICLE PERSONAL INJURY MOTOR VEHICLE PRODUCT LIABILITY
##   CT                           244                              17
##   MA                           196                              21
##   ME                            44                              11
##   NH                            65                               2
##   RI                            47                               2
##   VT                            57                               3
##     
##      NEGOTIABLE INSTRUMENTS OCCUPATIONAL SAFETY/HEALTH OTHER CIVIL RIGHTS
##   CT                     22                          0               1513
##   MA                     88                          2               1455
##   ME                     10                          2                352
##   NH                      7                          1                266
##   RI                     11                          0                314
##   VT                      6                          0                201
##     
##      OTHER CONTRACT ACTIONS OTHER FRAUD OTHER LABOR LITIGATION
##   CT                   1066         202                     80
##   MA                   1991         250                    209
##   ME                    201          25                     15
##   NH                    237          56                     21
##   RI                    296          40                     17
##   VT                    154          18                      8
##     
##      OTHER PERSONAL INJURY OTHER PERSONAL PROPERTY DAMAGE
##   CT                   675                             93
##   MA                   804                            259
##   ME                   144                             20
##   NH                   153                             36
##   RI                   149                             25
##   VT                   110                             17
##     
##      OTHER REAL PROPERTY ACTIONS OTHER STATUTORY ACTIONS
##   CT                          74                     316
##   MA                         296                     803
##   ME                          25                      68
##   NH                          52                     138
##   RI                          66                     111
##   VT                          10                      46
##     
##      PERSONAL INJURY -PRODUCT LIABILITY PROPERTY DAMAGE -PRODUCT LIABILTY
##   CT                                240                                69
##   MA                               2044                                90
##   ME                                 78                                18
##   NH                                255                                16
##   RI                                347                                15
##   VT                                 24                                14
##     
##      RENT, LEASE, EJECTMENT SECURITIES, COMMODITIES, EXCHANGE
##   CT                     10                               109
##   MA                     28                               270
##   ME                      1                                 2
##   NH                      8                                18
##   RI                      8                                 4
##   VT                      1                                 9
##     
##      STOCKHOLDER'S SUITS TORT PRODUCT LIABILITY TORTS TO LAND TRADEMARK
##   CT                  31                     30            15       209
##   MA                  87                    162            21       450
##   ME                   0                      1             5        33
##   NH                   4                     28            11        27
##   RI                  12                     21            10        42
##   VT                   7                      4            11        27
##     
##      TRUTH IN LENDING
##   CT               36
##   MA               38
##   ME                3
##   NH                3
##   RI               11
##   VT                1

Q4 What is the district that handles the largest number of civil cases?

Revise the barchart code below to find the answer.

The district that handles the largest number of civil cases is MA

ggplot(civilCases, aes(x = DISTRICT)) + 
  geom_bar()

Q5 Revise the code as instructed below. Don’t be surprised if your chart doesn’t look right. Explain the reason why.

Map district to the x-axis and nature of suit to color.

There is alot of variables which makes it hard to make a chart

ggplot(civilCases, aes(x = DISTRICT, fill = nature_of_suit)) + 
  geom_bar(position = "fill") #position = "fill", to have a stacked barchart

Q6 Which district has the largest share of HEALTH CARE / PHARM cases?

Simplify the chart in Q5 by filtering for the top five categories in the nature of suit:

The district with the largest share of HEALTH CARE / PHARM cases is MA

  1. HEALTH CARE / PHARM
  2. OTHER CIVIL RIGHTS
  3. OTHER CONTRACT ACTIONS
  4. CIVIL RIGHTS JOBS
  5. PERSONAL INJURY -PRODUCT LIABILITY

Revise the filter code below.

civilCases_filtered <- 
  civilCases %>% 
  filter(nature_of_suit %in% c("HEALTH CARE / PHARM", " OTHER CIVIL RIGHTS", "OTHER CONTRACT ACTIONS", "CIVIL RIGHTS JOBS", "PERSONAL INJURY -PRODUCT LIABILITY"))

ggplot(civilCases_filtered, aes(x = DISTRICT, fill = nature_of_suit)) + 
  geom_bar(position = "fill") #position = "fill", to have a stacked barchart