Hi Eric,

Thanks for your quick response.  It works.

Have a nice weekend!

Aiguo

From: Eric Berger <ericjber...@gmail.com>
Date: Friday, February 18, 2022 at 3:35 PM
To: "Li, Aiguo (NIH/NCI) [E]" <l...@mail.nih.gov>
Cc: "r-help@r-project.org" <r-help@r-project.org>
Subject: [EXTERNAL] Re: [R] A question of data frame filter based on another one

CAUTION: This email originated from outside of the organization. Do not click 
links or open attachments unless you recognize the sender and are confident the 
content is safe.

x[apply(y,MAR=1,sum) > 0,]


On Fri, Feb 18, 2022 at 10:24 PM Li, Aiguo (NIH/NCI) [E] via R-help 
<r-help@r-project.org<mailto:r-help@r-project.org>> wrote:
I have tow dataframes as below:
> x
  id  g
1  1 21
2  3 52
3  2 43
4  4 94
5  5 35

> y
  id g
1  1 1
2  0 0
3  0 1
4  1 0
5  1 0

Results dataframe I want is:
1 21
2 43
4 94
5 35

Basically I want to extract all the values in x which corresponding those 
values =1 in y.

I tried:
x[which(y==1),].  It gets:
id  g
1     1 21
4     4 94
5     5 35
NA   NA NA
NA.1 NA NA

But missing the row: 2 43.

Any help will be appreciated.

Thanks,
Aiguo

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org<mailto:R-help@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@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.

Reply via email to