Reputation: 1129
I am using the following command that returns this output:
> table(data$Smoke, data$Gender)
female male
no 314 334
yes 44 33
Nonetheless, in the tutorial I'm watching, the instructor uses the same line of code and they get
Gender
Smoke female male
no 314 334
yes 44 33
How can I achieve this result? It's not clear from the help menu.
Upvotes: 2
Views: 335
Reputation: 35604
Just pass a two-column data.frame
object to table()
table(data[c("Smoke", "Gender")])
# Gender
# Smoke female male
# no 29 31
# yes 17 23
or use xtabs()
:
xtabs( ~ Smoke + Gender, data)
# Gender
# Smoke female male
# no 29 31
# yes 17 23
Although the following one also works, it looks some rude.
table(Smoke = data$Smoke, Gender = data$Gender)
Data
data <- data.frame(id = 1:100,
Smoke = sample(c("no", "yes"), 100, T),
Gender = sample(c("female", "male"), 100, T))
Upvotes: 2
Reputation: 4243
You can name the vectors you pass to table
.
table(Smoke = c('no','yes'), Gender = c('male','female'))
#-----
Gender
Smoke female male
no 0 1
yes 1 0
Upvotes: 2