[R] Determining a basal correct count
Phil Spector
spector at stat.berkeley.edu
Fri Oct 29 01:58:33 CEST 2010
David -
I think changing
apply(x,1,function(x)rle(x[which(x==1)[1]:length(x)])$lengths[1])
to
apply(x,1,function(x)if(!any(x==1)) 0 else rle(x[which(x==1)[1]:length(x)])$lengths[1])
solves the problem.
- Phil
On Thu, 28 Oct 2010, David Herzberg wrote:
> Thank you Phil - I'll give this a try. I do have some empty rows, so I'll have
> to deal with that eventually.
>
> Dave
>
> Sent via DROID X
>
>
> -----Original message-----
> From: Phil Spector <spector at stat.berkeley.edu>
> To: David Herzberg <davidh at wpspublish.com>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Sent: Thu, Oct 28, 2010 23:39:34 GMT+00:00
> Subject: Re: [R] Determining a basal correct count
>
> David -
> I *think*
>
> apply(x,1,function(x)rle(x[which(x==1)[1]:length(x)])$lengths[1])
>
> gives you what you want, but without a reproducible example it's
> hard to say. It will fail if there are no 1s in a given row.
>
> - Phil Spector
> Statistical Computing Facility
> Department of Statistics
> UC Berkeley
> spector at stat.berkeley.edu
>
>
> On Thu, 28 Oct 2010, David Herzberg wrote:
>
> > Here's another interesting problem: if you recall I have a data frame
> (LCvars1) that consists of about 1500 cases (rows) of data from kids who
> took a test of listening comprehension. The columns are their scores (1
> = correct, 0 = incorrect, . = missing) on 140 test items. The items are
> numbered sequentially and are ordered by increasing difficulty as you go
> from left to right across the columns.
> >
> > I used the following (thanks to Peter Ehlers for this solution):
> >
> > First1ItemNo <- as.vector(
> > apply(
> > LCvars1, 1, match, x=1
> > ))
> >
> > to make R go through the columns from left to right and record into a
> vector the column number of the first '1' response for each case.
> >
> > Now, for each case (row), I want R to START with the column that
> contains the first '1' response, and continue to the right and count the
> number of consecutive columns containing '1' responses. At the next '0'
> or '.', I want R to record the count of consecutive '1's, and the skip
> to the next row and begin the process anew.
> >
> > Thanks in advance for your help,
> >
> > David S. Herzberg, Ph.D.
> > Vice President, Research and Development
> > Western Psychological Services
> > 12031 Wilshire Blvd.
> > Los Angeles, CA 90025-1251
> > Phone: (310)478-2061 x144
> > FAX: (310)478-7838
> > email: davidh at wpspublish.com<mailto:davidh at wpspublish.com>
> >
> >
> >
> > [[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