[R] ifelse to speed up loop?

Bert Gunter gunter.berton at gene.com
Fri Jan 25 00:39:14 CET 2013


I should add that if your V1 column is numeric, fiddling around with
?diff or equivalent will also identify the groupings for you and may
be faster than rle().

-- Bert

On Thu, Jan 24, 2013 at 3:32 PM, Bert Gunter <bgunter at gene.com> wrote:
> Your query is a little unclear to me, but I suspect
> ?rle
> is what you want.
>
> -- Bert
>
> On Thu, Jan 24, 2013 at 1:20 PM, Jeffrey Fuerte <fuertejn at vcu.edu> wrote:
>> Hello,
>>
>> I'm not sure how to explain what I'm looking for but essentially I have a
>> test dataset that looks like this:
>>
>>  test:
>>    V1
>> 1   1
>> 2   1
>> 3   1
>> 4   2
>> 5   2
>> 6   2
>> 7   1
>> 8   1
>> 9   1
>> 10  2
>> 11  2
>> 12  2
>>
>> And what I want to be able to do is create another column that captures a
>> "grouping" variable that looks like this:
>>
>>  test
>>    V1 group
>> 1   1  1
>> 2   1  1
>> 3   1  1
>> 4   2  2
>> 5   2  2
>> 6   2  2
>> 7   1 3
>> 8   1  3
>> 9   1  3
>> 10  2  4
>> 11  2  4
>> 12  2  4
>>
>> So, it's keeping track of the changes in V1, but even though V1 could be
>> the same in different instances, the group is treating it as a new group.
>>  I have written a loop that does what I want, but this takes too long to
>> run, so I was hoping for either a faster approach or an ifelse statement.
>>  Any ideas?
>>
>> By the loop I was using looks like this:
>>
>> groupings <- 1
>> test$group[1] <- groupings
>> for (i in 2:length(test$V1))
>> {
>> if (test$V1[i]==test$V1[i-1])
>> {
>> test$group[i] <- groupings
>> } else {
>> groupings <- groupings+1
>> test$group[i] <- groupings
>> }
>> }
>>
>> Thanks for the help.
>>
>>         [[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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list