library(dplyr)
## 
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(scales)
df<-read.csv('FATAL ENCOUNTERS DOT ORG SPREADSHEET (See Read me tab) - Form Responses.csv')

1. correlatoin between hignest level of force and whether individual flees

unique(df$Fleeing.Not.fleeing) 
##  [1] ""                                          
##  [2] "Not fleeing"                               
##  [3] "Duplicate of 13419?"                       
##  [4] "Duplicate of 13457?"                       
##  [5] "Fleeing/Motorcycle"                        
##  [6] "Fleeing/Vehicle"                           
##  [7] "Fleeing/Foot"                              
##  [8] "Uncertain"                                 
##  [9] "Fleeing/vehicle"                           
## [10] "Fleeing/Unspecified"                       
## [11] "Vehicle/Motorcycle"                        
## [12] "Vehicle/Fleeing"                           
## [13] "Fleeing/Veh"                               
## [14] "Vehicle"                                   
## [15] "Fleeing/Bicycle"                           
## [16] "Fleeing/Skateboard"                        
## [17] "Fleeling/Vehicle"                          
## [18] "Not Fleeing"                               
## [19] "Uncertain/Police alleged vehicular assault"
## [20] "None"                                      
## [21] "Motorcycle"                                
## [22] "Foot"                                      
## [23] "Firearm/Handgun"                           
## [24] "Fleeing/foot"                              
## [25] "Fleeing/Uncertain"                         
## [26] "Duplicate of 9914"
# replace value that contains "duplicate" with that of the corresponding row.
for (i in 1:nrow(df)) {
  if (grepl("Duplicate of", df$Fleeing.Not.fleeing[i])) {
    dupid <- as.numeric(sub("Duplicate of ", "", df$Fleeing.Not.fleeing[i]))
    replacement <- df$Fleeing.Not.fleeing[df$Unique.ID == dupid]
    if (length(replacement) > 0) {
      df$Fleeing.Not.fleeing[i] <- replacement
    }
  }
}
## Warning: 强制改变过程中产生了NA
## Warning in df$Fleeing.Not.fleeing[i] <- replacement:
## 被替换的项目不是替换值长度的倍数
## Warning: 强制改变过程中产生了NA
## Warning in df$Fleeing.Not.fleeing[i] <- replacement:
## 被替换的项目不是替换值长度的倍数

## Warning in df$Fleeing.Not.fleeing[i] <- replacement:
## 被替换的项目不是替换值长度的倍数
unique(df$Fleeing.Not.fleeing) 
##  [1] ""                                          
##  [2] "Not fleeing"                               
##  [3] NA                                          
##  [4] "Fleeing/Motorcycle"                        
##  [5] "Fleeing/Vehicle"                           
##  [6] "Fleeing/Foot"                              
##  [7] "Uncertain"                                 
##  [8] "Fleeing/vehicle"                           
##  [9] "Fleeing/Unspecified"                       
## [10] "Vehicle/Motorcycle"                        
## [11] "Vehicle/Fleeing"                           
## [12] "Fleeing/Veh"                               
## [13] "Vehicle"                                   
## [14] "Fleeing/Bicycle"                           
## [15] "Fleeing/Skateboard"                        
## [16] "Fleeling/Vehicle"                          
## [17] "Not Fleeing"                               
## [18] "Uncertain/Police alleged vehicular assault"
## [19] "None"                                      
## [20] "Motorcycle"                                
## [21] "Foot"                                      
## [22] "Firearm/Handgun"                           
## [23] "Fleeing/foot"                              
## [24] "Fleeing/Uncertain"
# deal with some NA
df$Fleeing.Not.fleeing[df$Fleeing.Not.fleeing == ""] <- NA
df$Highest.level.of.force[df$Highest.level.of.force == ""] <- NA

# subset df to 'flee' and 'notflee'

notflee=c("Not fleeing","Not Fleeing")
flee=c("Fleeing/Motorcycle","Fleeing/Vehicle","Fleeing/Foot" ,"Uncertain","Fleeing/vehicle","Fleeing/Unspecified","Vehicle/Motorcycle","Vehicle/Fleeing","Fleeing/Veh","Vehicle","Fleeing/Bicycle" ,"Fleeing/Skateboard","Fleeling/Vehicle","Motorcycle","Foot","Firearm/Handgun","Fleeing/foot","Fleeing/Uncertain")

df_flee <- df[df$Fleeing.Not.fleeing %in% flee, ]
df_notflee<-df[df$Fleeing.Not.fleeing %in% notflee,]
nrow(df)
## [1] 31498
nrow(df_flee)
## [1] 6169
nrow(df_notflee)
## [1] 10906
# show pie chart of highest level of force by flee or not

count1<-as.data.frame(table(df_flee$Highest.level.of.force))
count1$Per <- percent(count1$Freq / sum(count1$Freq),accuracy = 1)
count2<-as.data.frame(table(df_notflee$Highest.level.of.force))
count2$Per <- percent(count2$Freq / sum(count2$Freq),accuracy = 1)

p_flee <- ggplot(count1, aes(x = "", y = Freq,fill=Var1)) +
  geom_bar(stat = "identity") +
  coord_polar(theta = "y") +
  labs(title = "Highest level of force when flee") +
  geom_text(aes(label = Per),position = position_stack(vjust = 0.5))


p_notflee<-ggplot(count2, aes(x = "", y = Freq,fill=Var1)) +
  geom_bar(stat = "identity") +
  coord_polar(theta = "y") +
  labs(title = "Highest level of force when not flee")+
  geom_text(aes(label = Per),position = position_stack(vjust = 0.5))

# 打印图表
print(p_flee)

print(p_notflee)

From above, when individule flee, 57% of highest level of force are gunshot, 38% are vehicle. When individule not flee, 81% of highest level of force are gun shot, and 11% are vehicle. It probably suggest that, when individual flees, the police had a lower chance of shooting gun.

1. correlatoin between hignest level of force and year