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
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 >
Michael Friendly <friendly at yorku.ca> writes:> 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.If df is the name of the data frame you want df[rep(1:nrow(df), df$frequency), 1:2]
Something like that?> galton2<-galton[rep(1:nrow(galton), galton$frequency), 1:2]Christophe ----- Original Message ----- From: "Michael Friendly" <friendly at yorku.ca> To: "r-help" <r-help at stat.math.ethz.ch> Sent: Thursday, September 25, 2003 7:14 PM Subject: [R] ungrouping grouped data> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >