[R] A question of data frame filter based on another one

Eric Berger er|cjberger @end|ng |rom gm@||@com
Fri Feb 18 21:35:01 CET 2022


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 using 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 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