[R] Aggregating data help

Dennis Murphy djmuser at gmail.com
Fri Oct 21 01:17:32 CEST 2011


Hi:

Here's a way using the reshape2 package.

library('reshape2')
rsub <- subset(rtest, concept %in% c('8.2.D', '8.3.A', '8.3.B'))
# want year ahead of concept in the variable list
rsub <- rsub[, c(1:4, 9, 5:8)]

cast(rsub, id + test + subject + grade + year ~ concept, value_var = 'per_corr')
# Using per_corr as value column.  Use the value argument to cast to
override this choice
#   id test subject grade year 8.2.D 8.3.A 8.3.B
# 1  1   83 Mathema     8 2011   100    50   100
# 2  2   83 Mathema     8 2011   100   100    50

HTH,
Dennis

On Thu, Oct 20, 2011 at 11:55 AM, James Holland <holland.aggie at gmail.com> wrote:
> Hello,
>
> I have a dataset with student performance on a math test.  There are
> multiple cases for each student (identified by id) and the concept as a
> variable.
>
>> rtest
>
>     id   test subject     grade    concept   correct   tested per_corr
>  year
> 1    1   83   Mathema     8         8.2.D       1            1      100
>      2011
> 2    1   83   Mathema     8         8.3.A       1            2       50
>       2011
> 3    1   83   Mathema     8         8.3.B       2            2      100
>      2011
> 4    1   83   Mathema     8         8.4          2            2      100
>      2011
> 5    1   83   Mathema     8         8.5.A       1            2       50
>       2011
> 6    1   83   Mathema     8         8.5.B       0            2        0
>        2011
> 7    2   83   Mathema     8         8.2.D       1            1      100
>      2011
> 8    2   83   Mathema     8         8.3.A       2            2      100
>      2011
> 9    2   83   Mathema     8         8.3.B       1            2       50
>     2011
> 10  2   83   Mathema     8         8.4          2            2      100
>      2011
> 11  2   83   Mathema     8         8.5.A       1           2       50
>     2011
> 12  2   83   Mathema     8         8.5.B       0           2        0
>     2011
>
>
> I would like to make a variable for each concept (e.g. 8.2D, 8.3.A, 8.3B)
> and then put that percentage correct currently under per_corr for that
> variable.
>
> Such as
>
>      id   test    subject     grade      8.2D      8.3.A       8.3.B
> year
> 1    1      83   Mathema     8           100        50            100
>  2011
> 2    2      83   Mathema     8           100       100            50
> 2011
>
>
>
>
> Does anybody have an effcient way of doing this?
>
> What trips me up is handling the variables such as test and subject.
>
> Thank you,
>
> James
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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