filter and add a column

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

filter and add a column

anikaM
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: filter and add a column

Patrick (Malone Quantitative)
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: filter and add a column

anikaM
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) <
[hidden email]> 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 <[hidden email]>
> 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]]
> >
> > ______________________________________________
> > [hidden email] 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.
>
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: filter and add a column

Patrick (Malone Quantitative)
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 <[hidden email]> 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) <[hidden email]> 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 <[hidden email]> 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]]
>> >
>> > ______________________________________________
>> > [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: filter and add a column

anikaM
I really don't know how I would implement this

On Tue, Aug 6, 2019 at 1:42 PM Patrick (Malone Quantitative) <
[hidden email]> 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 <[hidden email]>
> 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) <
> [hidden email]> 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 <[hidden email]>
> 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]]
> >> >
> >> > ______________________________________________
> >> > [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: filter and add a column

Patrick (Malone Quantitative)
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 <[hidden email]> wrote:

>
> I really don't know how I would implement this
>
> On Tue, Aug 6, 2019 at 1:42 PM Patrick (Malone Quantitative) <[hidden email]> 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 <[hidden email]> 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) <[hidden email]> 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 <[hidden email]> 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]]
>> >> >
>> >> > ______________________________________________
>> >> > [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: filter and add a column

anikaM
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) <
[hidden email]> 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 <[hidden email]>
> wrote:
> >
> > I really don't know how I would implement this
> >
> > On Tue, Aug 6, 2019 at 1:42 PM Patrick (Malone Quantitative) <
> [hidden email]> 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 <[hidden email]>
> 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) <
> [hidden email]> 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 <
> [hidden email]> 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]]
> >> >> >
> >> >> > ______________________________________________
> >> >> > [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: filter and add a column

Eric Berger
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 <[hidden email]>
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) <
> [hidden email]> 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 <[hidden email]>
> > wrote:
> > >
> > > I really don't know how I would implement this
> > >
> > > On Tue, Aug 6, 2019 at 1:42 PM Patrick (Malone Quantitative) <
> > [hidden email]> 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 <
> [hidden email]>
> > 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) <
> > [hidden email]> 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 <
> > [hidden email]> 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]]
> > >> >> >
> > >> >> > ______________________________________________
> > >> >> > [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.