KVC_bioinfo
KVC_bioinfo

Reputation: 77

Keeping only specific entries with condition

I have a data set which looks like:

set ID   v1        
A1  222  1.86543   
B1  222  2.98764   
A1  234  3.76543   
B1  234  4.87654  
A1  235  1.98766  
B1  235  6.98765   
A1  21   1.98764   
B1  21   7.7654    

I am trying to keep only those entries where the difference between v1 values for A1 and B1 is greater than 2. I am a beginner in R and I do not have some solid code I tried to show here. Could someone please help me here?

Thanks in advance.

apologies about the change in question here. I presented the data incorrectly here. I have edited it now.

Upvotes: 0

Views: 27

Answers (2)

DJV
DJV

Reputation: 4863

Given the new data format, you can use the spread function

require(tidyverse) 

df %>% 
  spread(set, v1) %>% 
  filter(abs(A1-B1) > 2)

Result:

   ID      A1      B1
1  21 1.98764 7.76540
2 235 1.98766 6.98765

Data:

df <- read.table(text = "set ID   v1        
A1  222  1.86543   
           B1  222  2.98764   
           A1  234  3.76543   
           B1  234  4.87654  
           A1  235  1.98766  
           B1  235  6.98765   
           A1  21   1.98764   
           B1  21   7.7654 
           ", header = TRUE)

Upvotes: 0

cardinal40
cardinal40

Reputation: 1263

Looks like someone already got to this, but you could also use the abs function if the difference could be 2 in either direction.

library(tidyverse)

df <- tibble(
  set = c("A1", "B1", "A1", "B1"),
  ID = c(222, 222, 234, 234),
  v1 = c(1.86543, 2.98764, 3.76543, 4.87654),
  v2 = c(3.98765, 2.98764, 2.87643, 1.87653)
)

df %>% 
  filter(abs(v1 - v2) > 2)

Upvotes: 1

Related Questions