[英]table() function doesn't add number of males and females together in R (total beginner)
I want to create a frequency table that displays the following:我想创建一个显示以下内容的频率表:
I tried the following:我尝试了以下方法:
read.cvs2("Questionnaire")
data <- Questionnaire
students <- table(data$GENDER)
mStudents <- table(data$GENDER[data$GENDER == 1])
fStudents <- table(data$GENDER[data$GENDER == 2])
When I create the frequency table (as shown below) it the numbers are correct for male and female students but not for the total number of students.当我创建频率表(如下所示)时,男女学生的数字是正确的,但学生总数不正确。
freqTab <- cbind(students, mStudents, fStudents)
WHERE I BELIEVE THE ERROR IS:我认为错误在哪里:
in the environment pane under values, "students" this info: 'table' int [1:2 (1d)] and then the number of male and female students.在值下的环境窗格中,“学生”此信息:'table' int [1:2 (1d)],然后是男女学生的人数。 How can I make this one number?
我怎样才能使这个数字?
Would something like this work for you?像这样的东西对你有用吗? (Example with the built-in
mtcars
data set.) (以内置
mtcars
数据集为例。)
c(total = nrow(mtcars), table(mtcars$cyl))
## total 4 6 8
## 32 11 7 14
or或者
addmargins(table(mtcars$cyl))
## 4 6 8 Sum
## 11 7 14 32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.