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
                                         spec...@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: dav...@wpspublish.com<mailto:dav...@wpspublish.com>



        [[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.


______________________________________________
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.

Reply via email to