This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to filter rows based on valid value in each group

I'm working with proteomic data that have two conditions, one High-FCR group and Low FCR group, and three replicates each. I would like to filter valid values that have the minimum number of valid values at least two out of three replicates in each condition. Does anybody know how to change this code? because I tried to adapt R script by following [this link ][1] but it did not work (they filtering valid value at least two out of three replicates in at least one condition).

## Data filtering function
filter_valids = function(df, conditions, min_count, at_least_one = FALSE)

{# df = data frame containing LOG2 data for filtering and organized by data type
 # conditions = a character vector dictating the grouping
# min_count = a numeric vector of the same length as "conditions" indicating the minimum 
# number of valid values for each condition for retention
# at_least_one = TRUE means to keep the row if min_count is met for at least one condition
# FALSE means min_count must be met across all conditions for retention

log2.names = grep("^LOG2", names(df), value = TRUE) 
# Extract LOG2 column names
cond.names = lapply(conditions, # Group column names by conditions
            function(x) grep(x, log2.names, value = TRUE, perl = TRUE))

cond.filter = sapply(1:length(cond.names), function(i) {
    df2 = df[cond.names[[i]]]   # Extract columns of interest
    df2 = as.matrix(df2)   # Cast as matrix for the following command
    sums = rowSums(is.finite(df2)) # count the number of valid values for each condition
    sums >= min_count[i]   # Calculates whether min_count requirement is met
  })

if (at_least_one) {
    df$KEEP = apply(cond.filter, 1, any)
  } else {
    df$KEEP = apply(cond.filter, 1, all)
  }

  return(df)  
# No rows are omitted, filter rules are listed in the KEEP column }

## Apply filtering
df.F = filter_valids(df,
                     conditions = c("Parental", "Resistant"),
                     min_count = c(2, 2),
                     at_least_one = TRUE)

Thanks in advance.

proteomics r filter

0 answers

No answers yet.

Log in to answer this question.