jeudi 8 juin 2017

R - Subset a dataframe based on condition from another dataframe

I have a question which is somewhat similar to this post but it is not working for me because of a slight change in the condition.

I have 2 dataframes which looks like:

> effect
  V1        V2 V3
1 G1 activates G2
2 G3     stops G4
3 G5 activates G6
4 G6     stops G7

> expression
  V1 V2
1 G1  1
2 G2  0
3 G3  1
4 G4  0
5 G5  1
6 G6  1
7 G7  1

I want to keep only those rows in effect dataframe which fulfills following condition:

  • if effect$V2 == activates then expression$V2 should be 1 where effect$V3 and expression$V1 are the same

  • if effect$V2 == stops then expression$V2 should be 0 where effect$V3 and expression$V1 are the same

According to this criteria I will get on this results:

> results
  V1        V2 V3
1 G3     stops G4
2 G5 activates G6

Because this is consistent with the given conditions such that for the first row the V2=stops and expression$V2 for the G4 is 0.

The structure of the dataframes are given below. Any help will be much appreciated.

Similarly, for row2 V2=activatesand expression$V2 for the G6 is 1

df1:

structure(list(V1 = structure(1:4, .Label = c("G1", "G3", "G5", 
"G6"), class = "factor"), V2 = structure(c(1L, 2L, 1L, 2L), .Label = c("activates", 
"stops"), class = "factor"), V3 = structure(1:4, .Label = c("G2", 
"G4", "G6", "G7"), class = "factor")), .Names = c("V1", "V2", 
"V3"), class = "data.frame", row.names = c(NA, -4L))

df2:

structure(list(V1 = structure(1:7, .Label = c("G1", "G2", "G3", 
"G4", "G5", "G6", "G7"), class = "factor"), V2 = c(1L, 0L, 1L, 
0L, 1L, 1L, 1L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-7L))

Aucun commentaire:

Enregistrer un commentaire