简体   繁体   English

table() 函数不会在 R 中将男性和女性的数量加在一起(总初学者)

[英]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:我想创建一个显示以下内容的频率表:

  1. Total number of students学生总数
  2. Male students男学生
  3. Female students女学生

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.

相关问题 R dplyr:在 group_by 中添加列以计算男性/女性的数量 - R dplyr: Add column in group_by to count number of males/females 如何在 r 中创建一个称为性别的向量,其中前 30 个人是男性,另一个是女性 - how to create a vector called genders in which the first 30 individuals are males and the other are females in r 如何分别绘制女性和男性的连续图? - How to plot a continuous graph for females and males separately? 如何分别对男性和女性进行多元回归? - How to run a multiple regression for males and females separately? 将平均值,总数和最大值放在r中的表中 - placing mean,total and max together in a table in r 初学者:R 自定义绘图函数没有运行所有行?? (Quantmod) - Beginner: R custom plot function doesn't run all lines?? (Quantmod) R函数ggplot和行在一起不起作用,每次发送不同的警告消息 - R function ggplot and lines together doesn't work, sending different warning messages each time 表 function 不适用于日期为 class - R 的日期 - table function doesn't work for dates with date class - R R在表格中添加元素数量 - R Add number of elements in a table 在 R 中,有没有办法计算一组 4 个值一起出现的次数,而组内值的顺序无关紧要? - In R, is there a way to count the number of times a group of 4 values occurred together where the order of the values within the group doesn't matter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM