I have got a dataframe containing measurement of aircraft noise like this:> Id <- c(1,4,5,2,3,6,4,1,2,5,6,3) > Noise <- c(88,94,97,98,92,56,103,102,87,95,92,97) > Height <- c(190, 150, 120, 115, 188, 104, 101, 189, 146, 111, 124,126)> > df <- data.frame(Id, Noise, Height)Now I would like to split this in two new dataframes. The first one containing the rows with the maximum Noise for each Id and in the second the other rows. I manage to find that maximum Noise levels...> m <- tapply(df$Noise, Id.factor, max) > m1 2 3 4 5 6 102 98 97 103 97 92 But how do I split my dataframe? -- Ed Gordijn Adviseur geluidscapaciteit Amsterdam Airport Schiphol Business Area Aviation (A/CAP/EC) Postbus 7501, 1118 ZG Schiphol Bezoekadres: Evert v/d Beekstraat 202, 1118 CP Schiphol tel 020 601 32 22 fax 020 601 21 34 e-mail gordijn_e@schiphol.nl meer informatie is te vinden op www.schiphol.nl -- ------------------------------------------------------------------- This e-mail may contain confidential and privileged material...{{dropped}}
Dimitris Rizopoulos
2006-Dec-07 09:08 UTC
[R] Splitting a dataframe at the results of tapply
try this: dat <- data.frame(Id, Noise, Height) ############## dat <- dat[order(dat$Id), ] ind <- unlist(tapply(dat$Noise, dat$Id, function(x) x == max(x))) dat[ind, ] dat[!ind, ] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "E. Gordijn" <GORDIJN_E at schiphol.nl> To: <r-help at stat.math.ethz.ch> Sent: Thursday, December 07, 2006 8:48 AM Subject: [R] Splitting a dataframe at the results of tapply>I have got a dataframe containing measurement of aircraft noise like > this: > >> Id <- c(1,4,5,2,3,6,4,1,2,5,6,3) >> Noise <- c(88,94,97,98,92,56,103,102,87,95,92,97) >> Height <- c(190, 150, 120, 115, 188, 104, 101, 189, 146, 111, 124, > 126) >> >> df <- data.frame(Id, Noise, Height) > Now I would like to split this in two new dataframes. The first one > containing the rows with the maximum Noise for each Id and in the > second > the other rows. > > I manage to find that maximum Noise levels... > >> m <- tapply(df$Noise, Id.factor, max) >> m > 1 2 3 4 5 6 > 102 98 97 103 97 92 > > But how do I split my dataframe? > > > -- > Ed Gordijn > Adviseur geluidscapaciteit > > Amsterdam Airport Schiphol > Business Area Aviation (A/CAP/EC) > Postbus 7501, 1118 ZG Schiphol > Bezoekadres: Evert v/d Beekstraat 202, 1118 CP Schiphol > > tel 020 601 32 22 > fax 020 601 21 34 > e-mail gordijn_e at schiphol.nl > > meer informatie is te vinden op www.schiphol.nl > -- > > > ------------------------------------------------------------------- > This e-mail may contain confidential and privileged > material...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
E. Gordijn
2006-Dec-07 10:15 UTC
[R] Betr.: Re: Splitting a dataframe at the results of tapply
Hi Dimitris, This works fine. Thanks!, Ed -- -- Ed Gordijn Adviseur geluidscapaciteit Amsterdam Airport Schiphol Business Area Aviation (A/CAP/EC) Postbus 7501, 1118 ZG Schiphol Bezoekadres: Evert v/d Beekstraat 202, 1118 CP Schiphol tel 020 601 32 22 fax 020 601 21 34 e-mail gordijn_e at schiphol.nl meer informatie is te vinden op www.schiphol.nl -- ------------------------------------------------------------------- This e-mail may contain confidential and privileged material...{{dropped}}