[R] How to calculate words in column?

David Winsemius dwinsemius at comcast.net
Sat Dec 6 18:53:19 CET 2008


On Dec 6, 2008, at 11:30 AM, Edwin Sendjaja wrote:

> Hi,
>
> I have a table for an 1 week exam result for many classes in school,  
> like
> this:
>
>       Day       Class_ID  Test    Result
> 1   Monday    1                  Paper Passed
> 2   Tuesday    1                  Oral   Passed
> 3   Friday       1                  Paper  Passed
> 4   Monday    3                  Paper Passed
> 5   Sunday     3                  Oral   Passed
> 6   Monday    3                  Paper Passed
> 6   Sunday     3                  Paper Passed
>
> How can I sum the Word "Passed" from Result column ( for earch  
> Class_ID and
> each Test), so i can get this following table:
>
>
>     Class_ID  Test          Passed_Count
> 1     1            Paper           2
> 2     1            Oral             1
> 3     3            Oral             1
> 4     3            Paper           3

Try

 > xtabs( formula=  ~ Test + Class_ID, data=cdat)
        Class_ID
Test    1 3
   Oral  1 1
   Paper 2 3
 > as.data.frame(xtabs( formula=  ~ Test + Class_ID, data=cdat))
    Test Class_ID Freq
1  Oral        1    1
2 Paper        1    2
3  Oral        3    1
4 Paper        3    3

This does not really sum the "Passed" column but rather counts the  
number of entries (which all happen to be "Passed" in your example  
data. (You also offered a data set with duplicate row numbers which  
was corrected.) Had you offered a dataset in which Result were  
somewhat more interesting, then counting the number of Passed might  
have required putting (Result=="Passed") on the left side of the  
formula.

-- 
David Winsemius


>
>
>
> Passed_Count column is just the Value, how many people passed the  
> exam. In the
> tabell, they are 2 people passed the paper test (Result=Passed).
>
>
> I hope someone can help me, Or maybe there is an example that i can  
> see.
>
>
> Thank you in advance
>
> ______________________________________________
> 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.



More information about the R-help mailing list