重新编码分类变量

时间:2019-06-08 20:55:19

标签: r grouping

我无法将变量“ Text_General_Code”中现有的较大“类别”分组。

我尝试单独处理“ Text_General_Code”。它给了我报告文件中的八个以上变量。

library(ggplot2)
library(lubridate)
library(zoo)
library(dplyr)
library(knitr)
library(plotly)

# Read csv in R
## 
pdx = read.csv("https://cyo.arringtonadventures.com/crime/crime.csv",header = T)
head(pdx)

# Create a variable count with value 1
pdx$Count <- 1

# Convert Date from factor to date
#pdx$Date <- mdy_hms(pdx$Dispatch_Date_Time)

# Extract year from Date
pdx$Year <- substring(pdx$Dispatch_Date,1,4)

# Rename District from Dc_Dist 
colnames(pdx)[1] <- "District"

# Drop all variables we are not interested in
#select(pdx, -2,-3,-5,-7,-8,-9,-11,-12,-13,-14)

# Group Text_General_Code by categories
pdx$Category[pdx$Text_General_Code == "THEFT" | pdx$Text_General_Code == "MOTOR VEHICLE THEFT"]  <- "Theft"
pdx$Category[pdx$Text_General_Code == "BATTERY"] <- "Battery"
pdx$Category[pdx$Text_General_Code == "CRIMINAL DAMAGE"] <- "Criminal damage"
pdx$Category[pdx$Text_General_Code == "NARCOTICS" | pdx$Text_General_Code == "OTHER NARCOTIC VIOLATION"] <- "Narcotics"
pdx$Category[pdx$Text_General_Code == "ASSAULT"] <- "Assault"
pdx$Category[pdx$Text_General_Code == "BURGLARY"] <- "Burglary"
pdx$Category[pdx$Text_General_Code == "ROBBERY"]  <- "ROBBERY"
pdx$Category[pdx$Text_General_Code == "ARSON" | pdx$Text_General_Code == "CONCEALED CARRY LICENSE VIOLATION" |
            pdx$Text_General_Code == "CRIMINAL TRESPASS" | pdx$Text_General_Code == "GAMBLINGS" |
            pdx$Text_General_Code == "HUMAN TRAFFICKING" | pdx$Text_General_Code == "INTERFERENCE WITH PUBLIC OFFICER" |
            pdx$Text_General_Code == "INTIMIDATION" | pdx$Type == "KIDNAPPING" | pdx$Type == "LIQUOR LAW VIOLATION" |
            pdx$Text_General_Code == "NON-CRIMINAL" | pdx$Text_General_Code == "NON - CRIMINAL" | 
            pdx$Text_General_Code == "OBSCENITY" | pdx$Text_General_Code == "OFFENSE INVOLVING CHILDREN"| 
            pdx$Text_General_Code == "PROSTITUTION" | pdx$Text_General_Code == "PUBLIC INDECENCY"| 
            pdx$Text_General_Code == "PUBLIC PEACE VIOLATION" | pdx$Text_General_Code == "STALKING"| 
            pdx$Text_General_Code == "WEAPONS VIOLATION"| pdx$Text_General_Code == "HOMICIDE" |
            pdx$Text_General_Code == "CRIM SEXUAL ASSAULT" | pdx$Text_General_Code == "SEX OFFENSE" |
            pdx$Text_General_Code == "DECEPTIVE PRACTICE" | pdx$Text_General_Code == "OTHER OFFENSE"] <- "Others"

我希望所有变量都归为变量“类别”。我只应该得到“殴打”,“电池”,“盗窃”,“刑事损坏”,“毒品”,“抢劫”,“盗窃”,其他所有东西都应该归为“其他”。我在“类别”变量中得到“ NA”。

注意:输入数据集有230万条记录,可能需要几分钟才能运行

1 个答案:

答案 0 :(得分:1)

首先,在read.csv语句中添加stringsAsFactors=F,以使其在使用时没有因子水平。另外,可能有助于确保Text_General_Code字段的大小写相同:

    pdx = read.csv("https://cyo.arringtonadventures.com/crime/crime.csv",header = T, stringsAsFactors=F) %>%
          mutate(Text_General_Code = str_to_sentence(Text_General_Code)) 

然后对Text_General_Code中的值进行计数,然后将其输出到可以检查的对象(假设您使用的是Rstudio):

   tgc <- pdx %>%
   count(Text_General_Code)
   view(tgc)

然后您会看到问题的一部分是您在#按类别分组Text_General_Code部分中正在寻找的东西实际上并不存在。还有一个“电池”根本不存在。

作为一种分组策略,您可能想尝试在dplyr链中使用case_when语句:

pdx <- pdx %>%
  mutate(category = case_when(Text_General_Code == "Thefts" | 
                              Text_General_Code == "Motor Vehicle Theft" |
                              Text_General_Code == "Theft from Vehicle" 
                               ~ "Theft",
                             Text_General_Code == "Robbery Firearm" | 
                              Text_General_Code == "Robbery No Firearm" 
                              ~ "Robbery"))

...等等,直到按照需要分组为止。

然后进行质量检查,进行检查:

pdx %>%
   count(category, Text_General_Code)