df <- data.frame( exp=c(1,1,2,2), name=c("gene1", "gene2", "gene1", "gene2"), value=c(1,1,3,-1) )
While trying to tune to dplyr
and reshape2
I came across a โsimpleโ way to select strings based on several conditions. If I want to have these genes (variable name
) that have value
above 0 in experiment 1 ( exp
== 1) and at the same time, value
below 0 in experiment 2; in df it will be "gene2". Of course, there must be many ways to do this, for example. a subset of df for each set of conditions (exp == 1 and value> 0 and exp == 2 and value <0), and then append the results of this subset:
library(dplyr) inner_join(filter(df,exp == 1 & value > 0),filter(df,exp == 2 & value < 0), by= c("name"="name"))[[1]]
Although this works, it looks very uncomfortable, and I feel that such conditional filtering is at the heart of reshape2
and dplyr
, but cannot figure out how to do this. Can someone enlighten me here?
source share