简体   繁体   English

根据数据表中的组和子组的计数创建变量

[英]Create variable based on counts of groups and sub groups in data table

I have many student records.我有很多学生记录。 I need to create two new variable.我需要创建两个新变量。 One should display the count of Unitcode (ie enrolments) for each student_ID for each Year .应该显示每个Year每个student_IDUnitcode (即注册)计数。

One should display the count of Fail (ie Grade=='Fail') for each student_ID for each Year .应该显示每个Year每个student_IDFail计数(即 Grade=='Fail')。 See the example of records for three students below:请参阅以下三个学生的记录示例:

   student_ID=c(rep("1001",8),rep("1002",3),rep("1005",11))
   Year=c(rep(2011,4),rep(2012,4),2011,2012,2013,rep(2011,4),rep(2012,3),rep(2013,4))
   Grade=c(rep("Fail",2),rep("Pass",3),rep("Fail",3),rep("Pass",7),rep("Fail",2),rep("Pass",5))
   Unitcode<-c(1201:1222)
   record<-data.table(student_ID, Year, Grade, Unitcode)

If someone could assist with counting new variables that would be greatly appreciated.如果有人可以协助计算新变量,将不胜感激。

A similar option using dplyr would be使用dplyr的类似选项是

library(dplyr)
record %>%
     group_by(student_ID, Year) %>%
     summarise(unitcodes=n(), fails=sum(Grade=='Fail'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM