R语言中基于行名(基因ID)的条件函数实现问题求助
Hey there! Let's work through your problems with handling the expression matrix and applying custom conditions—this stuff gets easier once you get the hang of R's vectorized operations (which are way more efficient than loops for most cases).
1. First: Master the Multi-Condition Check
Your core condition is: a gene must have values >1 in both SPECIES1 and SPECIES2, AND values <1 in all of SPECIES3 to SPECIES6. We can implement this without loops using R's row-wise operations.
First, let's define the two parts of your condition clearly:
- Condition 1: Values in SPECIES1 and SPECIES2 are both greater than 1
- Condition 2: Values in SPECIES3, SPECIES4, SPECIES5, SPECIES6 are all less than 1
Here's the code to compute these and get the matching genes:
# Define Condition 1: Both SPECIES1 and SPECIES2 > 1 cond1 <- exptable$SPECIES1 > 1 & exptable$SPECIES2 > 1 # Define Condition 2: All of SPECIES3-SPECIES6 < 1 # Using apply() to check "all" values per row cond2 <- apply(exptable[, c("SPECIES3", "SPECIES4", "SPECIES5", "SPECIES6")], 1, function(row) all(row < 1)) # Combine conditions to get genes that meet both requirements selected_genes <- rownames(exptable)[cond1 & cond2]
2. Handling Your Target Gene List
If you only want to check genes from your specific target list, we can first subset the expression matrix to those genes, then apply the same conditions:
# Let's say your target gene list is stored as: gene_list <- c("GENE1", "GENE2", "GENE3", "GENE4", "GENE5", "GENE6") # Subset the expression matrix to only include these genes target_subset <- exptable[rownames(exptable) %in% gene_list, ] # Re-apply the conditions to this subset cond1_target <- target_subset$SPECIES1 > 1 & target_subset$SPECIES2 > 1 cond2_target <- apply(target_subset[, 3:6], 1, function(row) all(row < 1)) # Get the genes from your list that meet the criteria selected_target_genes <- rownames(target_subset)[cond1_target & cond2_target]
If You Really Want to Use a Loop (For Learning Purposes)
While vectorized operations are better for performance, if you want to write a loop to understand the process step-by-step, here's how:
# Initialize an empty vector to store selected genes selected_genes_loop <- c() # Loop through each gene in your target list for(gene in gene_list) { # First, check if the gene exists in the expression matrix if(gene %in% rownames(exptable)) { # Extract the row for this gene goi <- exptable[gene, ] # Check both conditions meets_cond1 <- goi["SPECIES1"] > 1 & goi["SPECIES2"] > 1 meets_cond2 <- all(goi[c("SPECIES3", "SPECIES4", "SPECIES5", "SPECIES6")] < 1) # If both conditions are true, add the gene to our selected list if(meets_cond1 & meets_cond2) { selected_genes_loop <- c(selected_genes_loop, gene) } } else { # Optional: Warn if a gene from your list isn't found warning(paste("Gene", gene, "not present in exptable!")) } }
3. Write the Selected Genes to a File
Once you have your list of genes that meet the criteria, write them to a file with:
# Write to a text file (adjust the filename as needed) write.table(selected_target_genes, "selected_genes.txt", row.names = FALSE, col.names = FALSE, quote = FALSE)
A quick note: Looking at your sample exptable, none of the genes meet your exact conditions (GENE1 has all values =3, others have SPECIES3-SPECIES6 =5 which is >1), but the code will work correctly once your actual data has matching genes.
内容的提问来源于stack exchange,提问作者Tezie

