Dear Forum, I have a data frame as mydat = data.frame(basel_asset_class = c(2, 8, 8 ,8), defa_frequency = c(0.15, 0.07, 0.03, 0.001))> mydatbasel_asset_class defa_frequency 1 2 0.150 2 8 0.070 3 8 0.030 4 8 0.001 I need to get the subset of this data.frame where no of records for the given basel_asset_class is > 2, i.e. I need to obtain subset of above data.frame as (since there is only 1 record, against basel_asset_class = 2, I want to filter it)> mydat_abasel_asset_class defa_frequency 1 8 0.070 2 8 0.030 3 8 0.001 Kindly guide Katherine [[alternative HTML version deleted]]
Katherine, There are multiple ways to do this and I highly recommend you look into a basic R manual or search the forums. One quick example would be: mysub <- subset(mydat, basel_asset_class > 2) Cheers, Charles On Thu, Oct 17, 2013 at 1:55 AM, Katherine Gobin <katherine_gobin@yahoo.com>wrote:> Dear Forum, > > I have a data frame as > > mydat = data.frame(basel_asset_class = c(2, 8, 8 ,8), defa_frequency > c(0.15, 0.07, 0.03, 0.001)) > > > mydat > basel_asset_class defa_frequency > 1 2 0.150 > 2 8 0.070 > 3 8 0.030 > 4 8 0.001 > > > I need to get the subset of this data.frame where no of records for the > given basel_asset_class is > 2, i.e. I need to obtain subset of above > data.frame as (since there is only 1 record, against basel_asset_class = 2, > I want to filter it) > > > mydat_a > basel_asset_class defa_frequency > 1 8 0.070 > 2 8 0.030 > 3 8 0.001 > > Kindly guide > > Katherine > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >-- Charles Determan Integrated Biosciences PhD Candidate University of Minnesota [[alternative HTML version deleted]]
"Kindly guide" ... This is a very basic question, so the kindest guide I can give is to read an Introduction to R (ships with R) or a R web tutorial of your choice so that you can learn how R works instead of posting to this list. Cheers, Bert On Wed, Oct 16, 2013 at 11:55 PM, Katherine Gobin <katherine_gobin@yahoo.com> wrote:> Dear Forum, > > I have a data frame as > > mydat = data.frame(basel_asset_class = c(2, 8, 8 ,8), defa_frequency > c(0.15, 0.07, 0.03, 0.001)) > > > mydat > basel_asset_class defa_frequency > 1 2 0.150 > 2 8 0.070 > 3 8 0.030 > 4 8 0.001 > > > I need to get the subset of this data.frame where no of records for the > given basel_asset_class is > 2, i.e. I need to obtain subset of above > data.frame as (since there is only 1 record, against basel_asset_class = 2, > I want to filter it) > > > mydat_a > basel_asset_class defa_frequency > 1 8 0.070 > 2 8 0.030 > 3 8 0.001 > > Kindly guide > > Katherine > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >-- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 [[alternative HTML version deleted]]
> -----Original Message----- > > mydat > ? basel_asset_class defa_frequency > 1 ? ? ? ? ? ? ? ? 2 ? ? ? ? ?0.150 > 2 ? ? ? ? ? ? ? ? 8 ? ? ? ? ?0.070 > 3 ? ? ? ? ? ? ? ? 8 ? ? ? ? ?0.030 > 4 ? ? ? ? ? ? ? ? 8 ? ? ? ? ?0.001 > > > I need to get the subset of this data.frame where no of records for the > given basel_asset_class is > 2,Maybe something like subset(mydat, ave(1:nrow(mydat), base_asset_class, FUN=length)>2) ? S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
S Ellison
2013-Oct-18 14:12 UTC
[Rd] Possible tweak to R intro - was RE: [R] Subseting a data.frame -
Transferred from R-help:>> From: S Ellison >> Subsetting using subset() is perhaps the most natural way of >> subsetting data frames; perhaps a line or two and an example could >> usefully be included in the 'Working with data frames' section of the R >> Intro? > > From: Bert Gunter [mailto:gunter.berton at gene.com] > The R Intro Manual was largely or entirely the work of Bill Venables > some years ago. So it is not really a part of R's maintained document > system and has thus not been kept up to date with changes like the > convenience function, subset(), which is basically a wrapper for "[]" > . > > This is not to say that your suggestion is not worthwhile, only to > explain why it probably won't be acted upon. >It's trivial enough that I could offer a 3-line patch if someone has time and inclination to add it... S Ellison ******************************************************************* This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK