[R] A question of data frame filter based on another one
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sat Feb 19 00:38:25 CET 2022
Hello,
Sorry, typo. It's rowSums(y), not x.
x[rowSums(y) > 0L, ]
Rui Barradas
Às 20:30 de 18/02/2022, Rui Barradas escreveu:
> 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 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.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list