Kevin Burnham
2011-Mar-24 19:24 UTC
[R] How create vector that sums correct responses for multiple subjects?
I have a data file with indicates pretest scores for a linguistics experiment. The data are in long form so for each of 33 subjects there are 400 rows, one for each item on the test, and there is a column called ‘Correct’ that shows ‘C’ for a correct response and ‘E’ for an incorrect response. I am trying to write a formula that will create a vector that indicates the number of correct answers for each subject. The following: nrow(pretestdata[(pretestdata$Subject=="1" & pretestdata$Correct=="C"),]) gives the number of correct responses for subject 1, but I would like a vector that indicates the number correct for each of 33 subjects. Thank you sincerely for your assistance. Kevin Burnham [[alternative HTML version deleted]]
Sarah Goslee
2011-Mar-24 20:16 UTC
[R] How create vector that sums correct responses for multiple subjects?
Try this: tapply(mydata$Correct, mydata$Subject, function(x)sum(x == "C")) Sarah On Thu, Mar 24, 2011 at 3:24 PM, Kevin Burnham <kburnham at gmail.com> wrote:> I have a data file with indicates pretest scores for a linguistics > experiment. ?The data are in long form so for each of 33 subjects there are > 400 rows, one for each item on the test, and there is a column called > ?Correct? that shows ?C? for a correct response and ?E? for an incorrect > response. ?I am trying to write a formula that will create a vector that > indicates the number of correct answers for each subject. > > > > The following: > > > > nrow(pretestdata[(pretestdata$Subject=="1" & pretestdata$Correct=="C"),]) > > > > gives the number of correct responses for subject 1, but I would like a > vector that indicates the number correct for each of 33 subjects. > > > > ?Thank you sincerely for your assistance. > > > > Kevin Burnham >-- Sarah Goslee http://www.functionaldiversity.org
Jorge Ivan Velez
2011-Mar-24 20:22 UTC
[R] How create vector that sums correct responses for multiple subjects?
Hi Kevin, Try this (untested): sapply(split(pretestdata, Subject), function(l) with(l$Correct == "C")) HTH, Jorge On Thu, Mar 24, 2011 at 3:24 PM, Kevin Burnham <> wrote:> I have a data file with indicates pretest scores for a linguistics > experiment. The data are in long form so for each of 33 subjects there are > 400 rows, one for each item on the test, and there is a column called > ‘Correct’ that shows ‘C’ for a correct response and ‘E’ for an incorrect > response. I am trying to write a formula that will create a vector that > indicates the number of correct answers for each subject. > > > > The following: > > > > nrow(pretestdata[(pretestdata$Subject=="1" & pretestdata$Correct=="C"),]) > > > > gives the number of correct responses for subject 1, but I would like a > vector that indicates the number correct for each of 33 subjects. > > > > Thank you sincerely for your assistance. > > > > Kevin Burnham > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]
Hadley Wickham
2011-Mar-24 20:46 UTC
[R] How create vector that sums correct responses for multiple subjects?
On Thu, Mar 24, 2011 at 2:24 PM, Kevin Burnham <kburnham at gmail.com> wrote:> I have a data file with indicates pretest scores for a linguistics > experiment. ?The data are in long form so for each of 33 subjects there are > 400 rows, one for each item on the test, and there is a column called > ?Correct? that shows ?C? for a correct response and ?E? for an incorrect > response. ?I am trying to write a formula that will create a vector that > indicates the number of correct answers for each subject. > > nrow(pretestdata[(pretestdata$Subject=="1" & pretestdata$Correct=="C"),]) > > > > gives the number of correct responses for subject 1, but I would like a > vector that indicates the number correct for each of 33 subjects.How about with(pretestdata, table(Subject, Correct)) ? Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/