1

I'm pretty new to coding in R and am doing an assignment where my task is to create a contingency table. My sample data of News Inquiries in Texas examines the variables: Day,Section(News, Sports ,or Business), and number of Inquiries. From what I have looked up I should be able to use the table() command but that doesn't seem to be working. My code is as follows:

    NewsData <- read.csv(file.choose(),header=TRUE,sep=',')

    table(NewsData$Day,NewsData$Section)

But when I run it it returns this.

    '''
                Business News Sports
      Monday           4    4      4
      Tuesday          4    4      4
      Wednesday        4    4      4
      Thursday         4    4      4
      Friday           4    4      4
    '''

I have checked the numbers and those are not accurate. I'm not sure what its doing. Is there something I'm missing or forgetting to do?

Edit: I have attached a sample of my data using:

    dput(NewsData[1:20,])

The output is as follows:

    structure(list(X = 1:20, Day = structure(c(1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), 
    .Label = c("Monday", 
    "Tuesday", "Wednesday", "Thursday", "Friday"), class = "factor"), 
        Inquiries = c(14L, 11L, 9L, 11L, 12L, 13L, 13L, 15L, 11L, 
        12L, 12L, 14L, 7L, 8L, 6L, 8L, 16L, 15L, 14L, 17L), Section = 
    c("News", 
        "News", "News", "News", "News", "News", "News", "News", 
    "News", 
        "News", "News", "News", "News", "News", "News", "News", 
    "News", 
        "News", "News", "News")), row.names = c(NA, 20L), class = 
    "data.frame")
  • Hi Kyle Schichl, welcome to Stack Overflow. It will be much easier to help if you provide at least a sample of your data with `dput(NewsData)` or if your data is very large `dput(NewsData[1:20,])`. Keep in mind if your class has an honor policy, posting the data here may reveal your violation of that code. You can [edit] your question and paste the output. Please surround the output with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell Jun 16 '20 at 18:35
  • 1
    Thanks for the clarification. I have made those edits:) – Kyle Schichl Jun 16 '20 at 19:08
  • table counts the occurrences with no reagrd to the actual values. – Bernhard Jun 17 '20 at 05:16

1 Answers1

4

I think you're looking for xtabs.

The first argument is a formula that instructs xtabs what you want calculated. Think of ~ like an equals sign. So here, you want Inquiries calculated by Day and Section. As an aside, you could also write the formula as Inquiries ~ .. . stands for "everything else." The second argument of xtabs defines the data.

xtabs(Inquiries ~ Day + Section, NewsData)
           Section
Day         News
  Monday      45
  Tuesday     53
  Wednesday   49
  Thursday    29
  Friday      62
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
  • 1
    +1 Alternatively, depending on how one wants to go on analyzing the data `aggregate(NewsData$Inquiries, by = list(NewsData$Day, NewsData$Section), sum)` – Bernhard Jun 16 '20 at 19:19