[R] Filtering a dataframe

Saneesh C S c@@@nee@h@|e@ @end|ng |rom gm@||@com
Mon May 17 19:11:09 CEST 2021


Country <- c('Angola', 'Angola','Botswana',
'Botswana','Zimbabwe','Zimbabwe')
year <- c('2006', '2007', '2008', '2009', '2010', '2006')
bank_ratio <- c(24,25,38,34,42,49)
Reserve_ratio <- c(77,59,64,65,57,86)
broad_money <- c(163,188,317,361,150,288)
id <- (c(1,1,2,2,3,3))

df <- data.frame(Country, year, bank_ratio, Reserve_ratio,broad_money, id)

library(dplyr)

df1<- df %>%
  filter(id%in% c(1,2))



On Mon, May 17, 2021 at 5:13 PM Eric Berger <ericjberger using gmail.com> wrote:

> If your data frame is named x then
>
> y <- x[ x$id %in% c(1,2), ]
>
> would create a new data frame y that has what you want.
>
>
> On Mon, May 17, 2021 at 5:33 PM Admire Tarisirayi Chirume <
> atchirume using gmail.com> wrote:
>
> > Can someone help on how to filter my data frame below such that it
> retains
> > a country if a given id (last column) is satisfied eg filtering a data
> > frame that has countries with id 1 and 2 only
> >
> > Country year bank_ratio Reserve_ratio broad_money id
> > Angola 2006 24 77 163 1
> > Angola 2007 25 59 188 1
> > Botswana 2008 38 64 317 2
> > Botswana 2009 34 65 361 2
> > Zimbabwe 2010 42 57 150 3
> > Zimbabwe 2006 49 86 288 3
> >
> >
> >
> > Alternative email: addtarris using icloud.com/TChirume using rbz.co.zw
> > Skype: admirechirume
> > Call: +263773369884
> > whatsapp: +818099861504
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list