Seymoo
Seymoo

Reputation: 189

Condition based subset in R

I have 2 named numeric vectors of length 15000, met and nor where some similar names exist. for example:

head(met)
     ALB    IGKJ1     IGKC    IGKJ4    IGKJ2    IGHG2 
25.75415 20.55957 18.28749 17.87589 17.22944 16.60235
head(nor)
   SAA1      CRP  RNVU     SNORD68   CYP1A2     IGKJ1 
25.74548 24.05058 16.72566 15.05746 13.75348 10.74111

I want to subset met if it dose exist in nor and if each met value is 1.5*nor bigger than its corresponding nor value.

in the example above, by the comparison that I want IGKJ1 will be the only output.

How should I code this?

Upvotes: 0

Views: 67

Answers (1)

AntoniosK
AntoniosK

Reputation: 16121

library(dplyr)

# get named vectors
met = c(25.75415, 20.55957, 18.28749, 17.87589, 17.22944, 16.60235)
names(met) = c("ALB", "IGKJ1", "IGKC", "IGKJ4", "IGKJ2", "IGHG2")

nor = c(25.74548, 24.05058, 16.72566, 15.05746, 13.75348, 10.74111)
names(nor) = c("SAA1", "CRP", "RNVU", "SNORD68", "CYP1A2", "IGKJ1")

# transform them as data frames
dt_met = data.frame(v_met = met)
dt_met$names = row.names(dt_met)

dt_nor = data.frame(v_nor = nor)
dt_nor$names = row.names(dt_nor)

First option to keep names and both values as rows of a new dataframe:

# keep names as a dataset
dt_met %>%
  inner_join(dt_nor, by="names") %>%  # keep names that exist in both datsets
  filter(v_met > 1.5*v_nor) %>%       # keep rows where the condition is satisfied
  select(names, everything())         # order columns

#   names    v_met    v_nor
# 1 IGKJ1 20.55957 10.74111

Second option to keep only the names that pass your criteria and then use them to subset your original vector:

# save names as a vector
dt_met %>%
  inner_join(dt_nor, by="names") %>%  
  filter(v_met > 1.5*v_nor) %>%
  pull(names) -> new_names

# subset met using those names
met[names(met) %in% new_names]

#    IGKJ1 
# 20.55957 

Upvotes: 1

Related Questions