[R] filter and add a column

Eric Berger er|cjberger @end|ng |rom gm@||@com
Wed Aug 7 11:49:17 CEST 2019


Here is a different approach that does not involve dplyr or the creation of
the intermediate data frame 'controls'

f <- function(v) {
  any(v %in% c("E109","E119","E149")) & any(v %in% c("Caucasian")) &
    any(v %in% c("No kinship found","Ten or more third-degree relatives
identified"))
}

tot$controls <- ifelse( apply( tot,1,f), 1, -9 )

HTH,
Eric



On Tue, Aug 6, 2019 at 10:36 PM Ana Marija <sokovic.anamarija using gmail.com>
wrote:

> I guess this is what you meant:
>
>     controls$control <- rep(1,nrow(controls))
>     t1=merge(tot,controls, by="eid", all = T)
>
> On Tue, Aug 6, 2019 at 2:15 PM Patrick (Malone Quantitative) <
> malone using malonequantitative.com> wrote:
>
> > This should get you going:
> >
> > Add a vector of 1s to controls before joining the datasets.
> >
> > On Tue, Aug 6, 2019 at 3:01 PM Ana Marija <sokovic.anamarija using gmail.com>
> > wrote:
> > >
> > > I really don't know how I would implement this
> > >
> > > On Tue, Aug 6, 2019 at 1:42 PM Patrick (Malone Quantitative) <
> > malone using malonequantitative.com> wrote:
> > >>
> > >> See the posting guide for this list about plain text.
> > >>
> > >> Again, what have you tried? This is also mentioned in the posting
> guide.
> > >>
> > >>
> > >> On Tue, Aug 6, 2019 at 2:38 PM Ana Marija <
> sokovic.anamarija using gmail.com>
> > wrote:
> > >> >
> > >> > Hi Patrick,
> > >> >
> > >> > yes both controls and tot have "eid" column, please see attached
> > >> >
> > >> > Can you please tell em what means to post in "plain text" ?
> > >> >
> > >> > Thanks
> > >> > Ana
> > >> >
> > >> > On Tue, Aug 6, 2019 at 1:33 PM Patrick (Malone Quantitative) <
> > malone using malonequantitative.com> wrote:
> > >> >>
> > >> >> Do you have some kind of ID variable? If so, it should be
> > >> >> straightforward with the appropriate joining function.
> > >> >>
> > >> >> What have you tried?
> > >> >>
> > >> >> Also, please post in plain text.
> > >> >>
> > >> >>
> > >> >> On Tue, Aug 6, 2019 at 2:16 PM Ana Marija <
> > sokovic.anamarija using gmail.com> wrote:
> > >> >> >
> > >> >> > Hello,
> > >> >> >
> > >> >> > I am filtering my data frame "tot" via:
> > >> >> >
> > >> >> > controls=tot %>% filter_all(any_vars(. %in% c("E109",
> > "E119","E149"))) %>%
> > >> >> > filter_all(any_vars(. %in% c("Caucasian"))) %>%
> > filter_all(any_vars(. %in%
> > >> >> > c("No kinship found","Ten or more third-degree relatives
> > identified")))
> > >> >> >
> > >> >> > > dim(controls)
> > >> >> > [1] 15381  1093
> > >> >> > > dim(tot)
> > >> >> > [1] 502536   1093
> > >> >> >
> > >> >> > how do I add in my data frame "tot" a new column called
> "controls"
> > where
> > >> >> > every of those filtered 15381 rows would have the value 1 and the
> > rest
> > >> >> > which can be found in tot have the value -9?
> > >> >> >
> > >> >> > Thanks
> > >> >> > Ana
> > >> >> >
> > >> >> >         [[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