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

Li, Aiguo (NIH/NCI) [E] ||@| @end|ng |rom m@||@n|h@gov
Fri Feb 18 19:32:00 CET 2022


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



More information about the R-help mailing list