Dear list, given is the following data frame df(): Number Place Start End 1 218024740787 HHO 5 263 2008-01-02 00:21:14 2008-01-03 15:25:16 2 218024740787 HHO 5 263 2008-01-02 00:21:14 2008-01-02 00:21:14 3 318039091794 HHO 5 263 2008-01-02 00:21:14 2008-01-02 13:22:54 4 318039091794 HHO 5 263 2008-01-02 00:21:14 2008-01-02 00:21:14 5 318039379900 HHO 1 104 2008-01-02 06:45:01 2008-01-02 09:15:23 Now, I want to count the number of equal values of column "Start" but I also want the other columns to be preserved. Using: rle(as.character(df$Start)) -> m n <- data.frame(m$values, m$lengths) produces a list of items according to their frequency of the Start point: m.values m.lengths 1 2008-01-02 00:21:14 4 2 2008-01-02 06:45:01 1 I want now also other columns to be in this new data frame. It should look like that: Number Place m.values m.lengths 1 218024740787 HHO 5 263 2008-01-02 00:21:14 4 2 318039379900 HHO 1 104 2008-01-02 06:45:01 1 Does anybody can help me with this? Thanking you in advance! Ren? Sch?nemann -- ______________________________________________________ Technische Universit?t Berlin Institut f?r Land- und Seeverkehr Fachgebiet Schienenfahrwege und Bahnbetrieb Prof. Dr.-Ing. habil. J?rgen Siegmann Post Sekretariat SG 18 Salzufer 17-19 D-10587 Berlin Telefon +49 (0)30 314 - 23 314 Internet http://www.railways.tu-berlin.de ______________________________________________________ Dipl.-Verk.wirtsch. Ren? Sch?nemann - Wissenschaftlicher Mitarbeiter - Telefon +49 (0)30 314 - 22 710 Telefax +49 (0)30 314 - 25 530 E-Mail RSchoenemann at railways.tu-berlin.de ______________________________________________________ Technische Universit?t Berlin K?rperschaft ?ffentlichen Rechts Pr?sident Prof. Dr. Kurt Kutzler
Dear list, given is the following data frame df(): Number Place Start End 1 218024740787 HHO 5 263 2008-01-02 00:21:14 2008-01-03 15:25:16 2 218024740787 HHO 5 263 2008-01-02 00:21:14 2008-01-02 00:21:14 3 318039091794 HHO 5 263 2008-01-02 00:21:14 2008-01-02 13:22:54 4 318039091794 HHO 5 263 2008-01-02 00:21:14 2008-01-02 00:21:14 5 318039379900 HHO 1 104 2008-01-02 06:45:01 2008-01-02 09:15:23 Now, I want to count the number of equal values of column "Start" but I also want the other columns to be preserved. Using: rle(as.character(df$Start)) -> m n <- data.frame(m$values, m$lengths) produces a list of items according to their frequency of the Start point: m.values m.lengths 1 2008-01-02 00:21:14 4 2 2008-01-02 06:45:01 1 I want now also other columns to be in this new data frame. It should look like that: Number Place m.values m.lengths 1 218024740787 HHO 5 263 2008-01-02 00:21:14 4 2 318039379900 HHO 1 104 2008-01-02 06:45:01 1 Does anybody can help me with this? Thanking you in advance! Ren? Sch?nemann -- ______________________________________________________ Technische Universit?t Berlin Institut f?r Land- und Seeverkehr Fachgebiet Schienenfahrwege und Bahnbetrieb Prof. Dr.-Ing. habil. J?rgen Siegmann Post Sekretariat SG 18 Salzufer 17-19 D-10587 Berlin Telefon +49 (0)30 314 - 23 314 Internet http://www.railways.tu-berlin.de ______________________________________________________ Dipl.-Verk.wirtsch. Ren? Sch?nemann - Wissenschaftlicher Mitarbeiter - Telefon +49 (0)30 314 - 22 710 Telefax +49 (0)30 314 - 25 530 E-Mail RSchoenemann at railways.tu-berlin.de ______________________________________________________ Technische Universit?t Berlin K?rperschaft ?ffentlichen Rechts Pr?sident Prof. Dr. Kurt Kutzler
I think you can then use merge() to merge them back together n<- merge(n, df, by.x = c("m.values"), by.y = c("Start"), all.x = F, all.y = F) also see ?aggregate for a more efficient solution. HTH, Si. ----- Original Message ----- From: "Sch?nemann, Rene" <RSchoenemann at Railways.TU-Berlin.de> To: <R-help at r-project.org> Sent: Thursday, June 18, 2009 1:37 PM Subject: [R] filtering number of values in a data frame> Dear list, > > given is the following data frame df(): > > Number Place Start End > 1 218024740787 HHO 5 263 2008-01-02 00:21:14 2008-01-03 15:25:16 > 2 218024740787 HHO 5 263 2008-01-02 00:21:14 2008-01-02 00:21:14 > 3 318039091794 HHO 5 263 2008-01-02 00:21:14 2008-01-02 13:22:54 > 4 318039091794 HHO 5 263 2008-01-02 00:21:14 2008-01-02 00:21:14 > 5 318039379900 HHO 1 104 2008-01-02 06:45:01 2008-01-02 09:15:23 > > Now, I want to count the number of equal values of column "Start" but I > also want the other columns to be preserved. > > Using: > > rle(as.character(df$Start)) -> m > n <- data.frame(m$values, m$lengths) > > produces a list of items according to their frequency of the Start point: > > m.values m.lengths > 1 2008-01-02 00:21:14 4 > 2 2008-01-02 06:45:01 1 > > > I want now also other columns to be in this new data frame. It should look > like that: > > Number Place m.values m.lengths > 1 218024740787 HHO 5 263 2008-01-02 00:21:14 4 > 2 318039379900 HHO 1 104 2008-01-02 06:45:01 1 > > > Does anybody can help me with this? > > Thanking you in advance! > > Ren? Sch?nemann > > -- > ______________________________________________________ > > Technische Universit?t Berlin > Institut f?r Land- und Seeverkehr > Fachgebiet Schienenfahrwege und Bahnbetrieb > Prof. Dr.-Ing. habil. J?rgen Siegmann > > Post Sekretariat SG 18 > Salzufer 17-19 > D-10587 Berlin > > Telefon +49 (0)30 314 - 23 314 > > Internet http://www.railways.tu-berlin.de > ______________________________________________________ > > Dipl.-Verk.wirtsch. Ren? Sch?nemann > - Wissenschaftlicher Mitarbeiter - > > Telefon +49 (0)30 314 - 22 710 > Telefax +49 (0)30 314 - 25 530 > > E-Mail RSchoenemann at railways.tu-berlin.de > ______________________________________________________ > > Technische Universit?t Berlin > K?rperschaft ?ffentlichen Rechts > > Pr?sident Prof. Dr. Kurt Kutzler > > ______________________________________________ > 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. > >
This does exactly what is needed. Thanks guys! -----Original Message----- From: markleeds@verizon.net [mailto:markleeds@verizon.net] Sent: Thursday, June 18, 2009 5:30 PM To: rene.schoenemann@tu-berlin.de Subject: Re: [R] filtering number of values in a data frame do.call(rbind,lapply(unname(split(DF,list(DF$X1,DF$START),drop=TRUE)), function(.df) { data.frame(.df[1,1:4],count=nrow(.df)) })) On Jun 18, 2009, René Schönemann <rene.schoenemann@tu-berlin.de> wrote: Dear list, given is the following data frame df(): Number Place Start End 1 218024740787 HHO 5 263 2008-01-02 00:21:14 2008-01-03 15:25:16 2 218024740787 HHO 5 263 2008-01-02 00:21:14 2008-01-02 00:21:14 3 318039091794 HHO 5 263 2008-01-02 00:21:14 2008-01-02 13:22:54 4 318039091794 HHO 5 263 2008-01-02 00:21:14 2008-01-02 00:21:14 5 318039379900 HHO 1 104 2008-01-02 06:45:01 2008-01-02 09:15:23 Now, I want to count the number of equal values of column "Start" but I also want the other columns to be preserved. Using: rle(as.character(df$Start)) -> m n <- data.frame(m$values, m$lengths) produces a list of items according to their frequency of the Start point: m.values m.lengths 1 2008-01-02 00:21:14 4 2 2008-01-02 06:45:01 1 I want now also other columns to be in this new data frame. It should look like that: Number Place m.values m.lengths 1 218024740787 HHO 5 263 2008-01-02 00:21:14 4 2 318039379900 HHO 1 104 2008-01-02 06:45:01 1 Does anybody can help me with this? Thanking you in advance! René Schönemann -- ______________________________________________________ Technische Universität Berlin Institut für Land- und Seeverkehr Fachgebiet Schienenfahrwege und Bahnbetrieb Prof. Dr.-Ing. habil. Jürgen Siegmann Post Sekretariat SG 18 Salzufer 17-19 D-10587 Berlin Telefon +49 (0)30 314 - 23 314 Internet http://www.railways.tu-berlin.de ______________________________________________________ Dipl.-Verk.wirtsch. René Schönemann - Wissenschaftlicher Mitarbeiter - Telefon +49 (0)30 314 - 22 710 Telefax +49 (0)30 314 - 25 530 E-Mail RSchoenemann@railways.tu-berlin.de ______________________________________________________ Technische Universität Berlin Körperschaft öffentlichen Rechts Präsident Prof. Dr. Kurt Kutzler ______________________________________________ R-help@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. [[alternative HTML version deleted]]