[R] ungrouping grouped data

Thomas W Blackwell tblackw at umich.edu
Thu Sep 25 20:00:58 CEST 2003


Michael  -

 new <- as.data.frame(lapply(data, function(x,p) rep(x,p), data[["frequency"]]))

This should do it.  The first paragraph under "Details" in  help("rep")
says what  rep(x,p)  is doing above.  The rest is just hardware to apply
that to every column in your existing data frame, and turn the result
back into a data frame again.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Thu, 25 Sep 2003, Michael Friendly wrote:

> I'm sure this is probably simple, but I can't find an answer...
> I have a data frame (Galton's data on heights of parents and children),
> in grouped form,
>
> parent child frequency
> 73.5 72.2 1
> 73.5 73.2 3
> 72.5 68.2 1
> 72.5 69.2 2
> 72.5 70.2 1
> 72.5 71.2 2
> 72.5 72.2 7
> 72.5 73.2 2
> 72.5 74.2 4
> 71.5 65.2 1
> 71.5 66.2 3
> 71.5 67.2 4
> 71.5 68.2 3
> 71.5 69.2 5
> 71.5 70.2 10
> ...
>
> and need the ungrouped equivalent, where each input observation
> appears the number of times represented by its frequency.  In SAS
> this would be
>
> data galton2;
>     set galton;
>     do i=1 to frequency;
>        output;
>     end;
>
> I can replicate this in R, but surely there must be an easier way.
> (In APL2 it would be frequency\[1]galton)
>
> thanks,
> -Michael
> --
> Michael Friendly     Email: friendly at yorku.ca
> Professor, Psychology Dept.
> York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
> 4700 Keele Street    http://www.math.yorku.ca/SCS/friendly.html
> Toronto, ONT  M3J 1P3 CANADA
>




More information about the R-help mailing list