优化R中的倍数(如果不是)

时间:2018-12-03 01:18:58

标签: r

我是R的新手,他想弄清楚是否有一种方法可以简化以下代码

    assault<-"13"
drugs<-"35"
homicide<-"09"
kidnapping<-"10"
sex_offense<-"36"
sex_assault<-"11"
theft<-c("12","22","23","24","28")
crime_data$OffenseType<-ifelse((substr(crime_data$NCIC.CODE,1,2) %in% assault)==TRUE,"Assault",
                               ifelse((substr(crime_data$NCIC.CODE,1,2) %in% drugs)==TRUE,"Drugs",
                                      ifelse((substr(crime_data$NCIC.CODE,1,2) %in% homicide)==TRUE,"Homicide",
                                             ifelse((substr(crime_data$NCIC.CODE,1,2) %in% kidnapping)==TRUE,"Kidnapping",
                                                    ifelse((substr(crime_data$NCIC.CODE,1,2) %in% sex_offense)==TRUE,"Sexual Offense",
                                                           ifelse((substr(crime_data$NCIC.CODE,1,2) %in% sex_assault)==TRUE,"Sexual Assault",
                                                                  ifelse((substr(crime_data$NCIC.CODE,1,2) %in% theft)==TRUE,"Theft","Others")))))))

1 个答案:

答案 0 :(得分:0)

我不确定NCIC.CODE变量是如何创建的,因为它不在原始数据集中,但是正如前面提到的,dplyr::mutatedplyr::case_when的方法会沿用以下几行:

library(magrittr)
library(dplyr)
library(readr)

assault<-"13"
drugs<-"35"
homicide<-"09"
kidnapping<-"10"
sex_offense<-"36"
sex_assault<-"11"
theft<-c("12","22","23","24","28")

crime_data <- read_csv(file=file.choose())
crime_data %<>%
  mutate(NCIC.CODE = substr(NCIC.CODE,1,2))
  mutate(OffenseType = case_when(NCIC.CODE %in% assault ~ "Assault",
                             NCIC.CODE %in% homicide ~ "Homicide",
                             NCIC.CODE %in% kidnapping ~ "Kidnapping",
                             NCIC.CODE %in% sex_offense ~ "Sexual Offense",
                             NCIC.CODE %in% sex_assault ~ "Drugs",
                             NCIC.CODE %in% theft ~ "Sexual Assault",
                             NCIC.CODE %in% drugs ~ "Theft",
                             TRUE ~ "Others"
                             ))

如果您让我们知道如何创建NCIC.CODE,我们也许可以提供更多帮助。

如果您使用dput(head(crime_data, 10))并发布结果,它将显示一些数据