Removing or Setting pairs to NA based on conditions in another data frame (R)
1
0
Entering edit mode
2.3 years ago
selplat21 ▴ 20

I have two dataframes loaded in R:

File 1:

Sample1       Sample2        Metric

AAA        BBB        0.1

BBB        DDD        0.4

CCC        FFF        1.2

File 2

Sample        Site
AAA        A
BBB        C
CCC        B
DDD        C
EEE        A
FFF        B

I want to set the metric to NA in any rows in file 1 where both samples do not have the same Site in file 2.

R • 725 views
ADD COMMENT
3
Entering edit mode
2.3 years ago
mark.ziemann ★ 1.9k

Let's say these objects exist in your R session as file1 and file2, then you can use apply to loop over each row in file1 like this:

res <- apply(X=file1,MARGIN=1,FUN=function(x) { 
  S1=x[1]
  S2=x[2]
  S1F2 <- file2[which(file2[,"Sample"]==S1),"Site"]
  S2F2 <- file2[which(file2[,"Sample"]==S2),"Site"]
  if ( S1F2 == S2F2 ) {
    METRIC <- x[3]
  } else {
    METRIC <- NA
  }
  return(as.numeric(METRIC))
} )

file1$Metric2 <- res

The result will look like this:

  Sample1 Sample2 Metric Metric2
1     AAA     BBB    0.1      NA
2     BBB     DDD    0.4     0.4
3     CCC     FFF    1.2     1.2
ADD COMMENT
0
Entering edit mode

You rock. Thank you!

ADD REPLY

Login before adding your answer.

Traffic: 1685 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6