Thanks to rpolicastro and @zx8754 for your response. All 3 codes worked for me. I really appreciate your help!
Hi,
Above figure is an example of a large copy number ratio file in .csv format that I am currently trying to analyze. In this, I want to only selectively extract the genes that either have CN ratio above 2 or below 0.2 and extract all the column data for the entire row that is positive for the condition. In the example data above, I want to extract entire row data for gene 3, gene 4, gene 5, gene 6 for all the columns without specifying row names, but using >2 or <0.2 conditions as I am analyzing 20000 rows. Kindly let me know how to proceed with the same using R programming.
Thanks for your help in advance!
2 answers
Two tidyverse ways.
Load the required libraries and read in the data.
library("dplyr")
library("readr")
data <- read_csv("input.csv")
You can use rowwise.
filtered_data <- data |>
rowwise() |>
filter(any(c_across(!Genes) > 2 | c_across(!Genes) < 0.2)) |>
ungroup()
Or the more traditional data pivoting.
library("tidyr")
filtered_data <- data |>
pivot_longer(!Genes) |>
group_by(Genes) |>
filter(any(value > 2 | value < 0.2)) |>
ungroup() |>
pivot_wider(names_from=name, values_from=value)
Using base R, check every value if they meet the condition, then subset
data[ which(rowSums(data[, -1 ] > 2 | data[, -1 ] < 0.2) > 0), ]
Log in to answer this question.