# Re: [R] Count of rows while looping through data

From: Kenn Konstabel <lebatsnok_at_gmail.com>
Date: Wed, 25 May 2011 11:31:17 +0300

An alternative approach would be to `split` the data frame by family, then `lapply` a function selecting random row from each slice, and then `rbind` it all together.

x = data.frame(family = rep(1:20,sample(2:5,20,replace=TRUE)), xyz=1) randomrow <- function(x) x[sample(1:nrow(x),1),]

# step by step

```x.split <- split(x, x\$family)
x.rnd <- lapply(x.split, randomrow)
x.togetheragain <- do.call(rbind, x.rnd)

# or more concisely
do.call(rbind,      lapply(split(x, x\$family), randomrow)      )

```

Best regards,

Kenn

On Wed, May 25, 2011 at 12:54 AM, Phil Spector <spector_at_stat.berkeley.edu> wrote:
> Jeanna -
>    I can't imagine how you could solve this problem with a loop, but here's
> one way to solve it using R:
>
> First, I'll create a data frame with a family variable:
>
>> x = data.frame(family = rep(1:20,sample(2:5,20,replace=TRUE)))
>
> Next, I'll number each family member within each family:
>
>> x\$seq = ave(x\$family,x\$family,FUN=seq)
>
> Now I'll choose a random number within each family:
>
>> x\$use = ave(x\$family,x\$family,FUN=function(x)sample(1:length(x),1))
>
> Finally, I'll select the family member whose sequence number matches the
> random number:
>
>> answer = subset(x,seq == use)
>
> Hope this helps.  Take a look at the help page for the ave function
> to understand how it works.
>                                        - Phil Spector
>                                         Statistical Computing Facility
>                                         Department of Statistics
>                                         UC Berkeley
>                                         spector_at_stat.berkeley.edu
>
>
> On Tue, 24 May 2011, Jeanna wrote:
>
>> I have a data table with one column that indicates families, and
>> subsequent
>> columns with other characteristics.  I want to randomize one member of the
>> family to a separate table.  My approach is to count the number of
>> members,
>> set up a random number generator, and assign the family member based on
>> where they fall within the random number spectrum.
>>
>> Is there a way to count the number of family members as I loop through the
>> whole table?
>>
>> Something like this:
>> for (j in 1:15){
>>        if (x\$family[j] == x\$family[j+1]){
>>       count = count +1
>> (which doesn't work)
>>
>> as I do the larger:
>> for (i in 2:nrow(x.tab)){
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Count-of-rows-while-looping-through-data-tp3547949p3547949.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

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. Received on Wed 25 May 2011 - 08:43:53 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 26 May 2011 - 19:10:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.