Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selective Data Integration in R

Tags:

merge

dataframe

r

I am looking to integrate data from one dataframe (A) selectively into another (B). The conditions are as follows: The data frames share two columns (miRNA & Gene). Dataframe A also contains column with a value for the pair.

I want to create a new column in dataframe B that is taken from the Value column in A and contains a value if the pair (same miRNA & Gene from a row in A) matches in B. If a pair does not match in B, create a new row with the score.

Pseudocode

#Initialize column in B that will house A value if first two columns match
B$A_Values <- 0

If A[,1:2] == B[,1:2]:
     Change initialized B$A_Value to A[VALUE] of row from A[,1:2]

If A[,1,2] is not in B[,1:2]: 
     Add row in B[,1:2] 
     Change initialized B$A_Value to A[Value] of row from A[,1:2]

The dataframes are not of equal length and there will be items in B not found in A, though I assume my initialization will default value them to 0. Any help will be appreciated.

Cheers

like image 252
Cody Glickman Avatar asked Aug 22 '26 00:08

Cody Glickman


1 Answers

This is what the merge function does.

AB <- merge(A, B, by = c("miRNA", "Gene"), all = TRUE)

or if there are values in A that aren't in B and you want to remove those values, use

AB <- merge(A, B, by = c("miRNA", "Gene"), all.y = TRUE)
like image 65
shadowtalker Avatar answered Aug 24 '26 14:08

shadowtalker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!