Li, Aiguo (NIH/NCI) [E]
2022-Feb-18 18:32 UTC
[R] A question of data frame filter based on another one
I have tow dataframes as below:> xid g 1 1 21 2 3 52 3 2 43 4 4 94 5 5 35> yid 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]]
Rui Barradas
2022-Feb-18 20:30 UTC
[R] A question of data frame filter based on another one
Hello, Use ?rowSums and compare its result to 0. You want the sums greater than zero. 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" x <- read.table(textConnection(x), header = TRUE) y <- read.table(textConnection(y), header = TRUE) x[rowSums(x) > 0L, ] # id g #1 1 21 #2 3 52 #3 2 43 #4 4 94 #5 5 35 Hope this helps, Rui Barradas ?s 18:32 de 18/02/2022, Li, Aiguo (NIH/NCI) [E] via R-help escreveu:> 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 at 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.
Eric Berger
2022-Feb-18 20:35 UTC
[R] A question of data frame filter based on another one
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 at 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 at 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]]