-2

I have a data frame named orf. I am trying to work out coding on how to calculate the values for dependent variable in binary independent variable e.g Breed. The table gives me the number of breeds in each category. How do I go about coding to calculate for instance the 341 breed type 1 had 500 cases of disease. My y here is Cases

table(orf$Breed)
         1   2 
         341 405

    Example: 

Breed   Cases2012
    2   0
    1   0
    2   8
    2   73
    2   0
    2   26
    2   0
    2   45
    1   0
    2   22
    1   0
    1   0
    2   0
    2   6
    1   539
Joshua Onyango
  • 35
  • 4
  • 12

1 Answers1

1

This is one of those things there's probably a million ways of doing it in R.

Here's how I'd do it in dplyr

library(dplyr)

data %>%
  group_by(Breed) %>%
  summarise(cases = sum(Cases2012))
Mhairi McNeill
  • 1,951
  • 11
  • 20
  • here is what I tried initially in x tabs: xtabs(~Cases2012 +Breed , data = orf). However it doest give me the number of cases per breed – Joshua Onyango Jul 23 '15 at 13:16
  • Can you tell me more about the breed and the cases variable? If they factor variables and each observation refers to one case - if so you just need to tabulate them against each other – Mhairi McNeill Jul 23 '15 at 13:21
  • Here's some advice about making a reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Does table not work? – Mhairi McNeill Jul 23 '15 at 13:49
  • Table not work yet. See data sample from the main query post – Joshua Onyango Jul 23 '15 at 15:34
  • Okay, I've seen your data and I've changed my answer - try the new version – Mhairi McNeill Jul 23 '15 at 15:43
  • @ Mhairi McNeill rather than doing one variable at a time is there a way I could work out to get the values(cases) for several categorical variables? – Joshua Onyango Jul 29 '15 at 13:10