SHARE
TWEET

gendercause.R

ansakoy Oct 26th, 2013 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This function was written for working with this dataset:
  2. # https://spark-public.s3.amazonaws.com/compdata/data/Baltimore_homicides.zip
  3. # It extracts all cases of a given cause and returns a data frame with two
  4. # coulmns Cause and Gender.
  5.  
  6. gendercause <- function(data, cause) {
  7.   data <- readLines("homicides.txt")
  8.   if (cause == "asphyxiation") {
  9.     asph <- grep("[Cc]ause: [Aa]sphyxiation", data)
  10.     female <- grep("[Ff]emale", data[asph])
  11.     male <- grep(" [Mm]ale", data[asph])
  12.     cause <- character(0)
  13.     for (i in asph) {
  14.       i <- "asphyxiation"
  15.       cause <- c(i, cause)
  16.     }
  17.     genfem <- character(0)
  18.     for (i in female) {
  19.       i <- "female"
  20.       genfem <- c(i, genfem)
  21.     }
  22.     genmal <- character(0)
  23.     for (i in male) {
  24.       i <- "male"
  25.       genmal <- c(i, genmal)
  26.     }
  27.     gentotal <- c(genfem, genmal)
  28.     output <- data.frame(Cause = cause, Gender = gentotal)
  29.     return(output)
  30.   }
  31.   else if (cause == "other") {
  32.     oth <- grep("[Cc]ause: [Oo]ther", data)
  33.     female <- grep("[Ff]emale", data[oth])
  34.     male <- grep(" [Mm]ale", data[oth])
  35.     cause <- character(0)
  36.     for (i in oth) {
  37.       i <- "other"
  38.       cause <- c(i, cause)
  39.     }
  40.     genfem <- character(0)
  41.     for (i in female) {
  42.       i <- "female"
  43.       genfem <- c(i, genfem)
  44.     }
  45.     genmal <- character(0)
  46.     for (i in male) {
  47.       i <- "male"
  48.       genmal <- c(i, genmal)
  49.     }
  50.     gentotal <- c(genfem, genmal)
  51.     output <- data.frame(Cause = cause, Gender = gentotal)
  52.     return(output)
  53.   }
  54.   else if (cause == "blunt force") {
  55.     bf <- grep("[Cc]ause: [Bb]lunt.[Ff]orce", data)
  56.     female <- grep("[Ff]emale", data[bf])
  57.     male <- grep(" [Mm]ale", data[bf])
  58.     cause <- character(0)
  59.     for (i in bf) {
  60.       i <- "blunt force"
  61.       cause <- c(i, cause)
  62.     }
  63.     genfem <- character(0)
  64.     for (i in female) {
  65.       i <- "female"
  66.       genfem <- c(i, genfem)
  67.     }
  68.     genmal <- character(0)
  69.     for (i in male) {
  70.       i <- "male"
  71.       genmal <- c(i, genmal)
  72.     }
  73.     gentotal <- c(genfem, genmal)
  74.     output <- data.frame(Cause = cause, Gender = gentotal)
  75.     return(output)
  76.   }
  77.   else if (cause == "shooting") {
  78.     shoot <- grep("[Cc]ause: [Ss]hooting", data)
  79.     female <- grep("[Ff]emale", data[shoot])
  80.     male <- grep("[^t] [Mm]ale", data[shoot])
  81.     cause <- character(0)
  82.     for (i in shoot) {
  83.       i <- "shooting"
  84.       cause <- c(i, cause)
  85.     }
  86.     genfem <- character(0)
  87.     for (i in female) {
  88.       i <- "female"
  89.       genfem <- c(i, genfem)
  90.     }
  91.     genmal <- character(0)
  92.     for (i in male) {
  93.       i <- "male"
  94.       genmal <- c(i, genmal)
  95.     }
  96.     gentotal <- c(genfem, genmal)
  97.     output <- data.frame(Cause = cause, Gender = gentotal)
  98.     return(output)
  99.   }
  100.   else if (cause == "stabbing") {
  101.     stab <- grep("[Cc]ause: [Ss]tabbing", data)
  102.     female <- grep("[Ff]emale", data[stab])
  103.     male <- grep(" [Mm]ale", data[stab])
  104.     cause <- character(0)
  105.     for (i in stab) {
  106.       i <- "stabbing"
  107.       cause <- c(i, cause)
  108.     }
  109.     genfem <- character(0)
  110.     for (i in female) {
  111.       i <- "female"
  112.       genfem <- c(i, genfem)
  113.     }
  114.     genmal <- character(0)
  115.     for (i in male) {
  116.       i <- "male"
  117.       genmal <- c(i, genmal)
  118.     }
  119.     gentotal <- c(genfem, genmal)
  120.     output <- data.frame(Cause = cause, Gender = gentotal)
  121.     return(output)
  122.   }
  123.   else if (cause == "unknown") {
  124.     unknown <- grep("[Cc]ause: [Uu]nknown", data)
  125.     female <- grep("[Ff]emale", data[unknown])
  126.     male <- grep(" [Mm]ale", data[unknown])
  127.     cause <- character(0)
  128.     for (i in unknown) {
  129.       i <- "unknown"
  130.       cause <- c(i, cause)
  131.     }
  132.     genfem <- character(0)
  133.     for (i in female) {
  134.       i <- "female"
  135.       genfem <- c(i, genfem)
  136.     }
  137.     genmal <- character(0)
  138.     for (i in male) {
  139.       i <- "male"
  140.       genmal <- c(i, genmal)
  141.     }
  142.     gentotal <- c(genfem, genmal)
  143.     output <- data.frame(Cause = cause, Gender = gentotal)
  144.     return(output)
  145.   }
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top