Reputation: 31
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")
Upvotes: 1
Views: 67
Reputation: 24790
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
Upvotes: 4