[R] Count variables by ID

David Winsemius dwinsemius at comcast.net
Thu Feb 11 00:29:38 CET 2010


On Feb 10, 2010, at 5:58 PM, travelmail wrote:

>
> Hello,
>
> I have an education dataset and I'd like to know how to count the  
> number of
> courses each student has taken in a particular subject. For  
> instance, say
> the course codes for "History" are 20-26 & 31 and I have the  
> following data
>
> Student ID  	Course Code
> 1	                16
> 1	                17
> 1	                21
> 1	                31
>
> 2	                10
> 2	                22
> 2	                23
> 2	                24
>
>
> I would like a separate variable that reads like this
> Student ID    Hist Courses
> 1                  2
> 2                  3

 > table( courses[courses$CourseCode %in% c(20:26, 31), ]$StudentID)

1 2
2 3

 > data.frame( ctab)
   Var1 Freq
1    1    2
2    2    3

You can rename as you desire but please try to stop using spaces. Very  
unReliable.


>
> I've read other "count" tutorials on the forum, but couldn't still  
> find an
> answer to my problem. Thanks for helping out!
>
> Greg
> -- 
> View this message in context: http://n4.nabble.com/Count-variables-by-ID-tp1476618p1476618.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list