1 Section A — Arithmetic with Gene Expression Counts

1.1 Q1. Add Two Gene Counts.

gene_a <- 120
gene_b <- 200
combined_expression <- gene_a + gene_b
combined_expression
## [1] 320

1.2 Q2. Multiply Read Depths from Two Runs.

run1 <- 500000
run2 <- 600000
total_read_pairs <- run1 * run2
total_read_pairs
## [1] 3e+11

1.3 Q3. Calculate Fold-Change in Expression

control <- 300
treated <- 150
fold_change <- treated / control
fold_change
## [1] 0.5

1.4 Q4. Remainder of Reads per Lane

lane_capacity <- 250000
total_reads <- 1001000
leftover_reads <- total_reads %% lane_capacity
leftover_reads
## [1] 1000

1.5 Q5. Store a Gene Count in a Variable

gene_x_count <- 350
gene_x_count
## [1] 350

1.6 Q6. Add Counts from Two Patients

patient_a <- 100
patient_b <- 120
total_expression <- patient_a + patient_b
total_expression
## [1] 220

1.7 Q7. Update a Mistaken Value

patient_b_updated <- 130
total_expression <- patient_a + patient_b_updated
total_expression
## [1] 230

1.8 Q8. Store the Name of a Marker Gene

marker_gene <- "CD274"
marker_gene
## [1] "CD274"

2 Section B — Working with Data Types

2.1 Q9. Check the Data Type of a Count

x <- 100
class(x)
## [1] "numeric"

2.2 Q10. Check the Type of a Gene Name

gene_name <- "TP53"
class(gene_name)
## [1] "character"

2.3 Q11. Logical Flag for Marker Gene

is_marker <- TRUE
class(is_marker)
## [1] "logical"

3 Section C — Vectors for Gene Expression Data

3.1 Q12. Create a Vector of Gene Counts

counts <- c(120, 150, 130)
counts
## [1] 120 150 130

3.2 Q13. Assign Gene Names to Counts

names(counts) <- c("TP53", "BRCA1", "EGFR")
counts
##  TP53 BRCA1  EGFR 
##   120   150   130

3.3 Q14. Print the Named Vector

print(counts)
##  TP53 BRCA1  EGFR 
##   120   150   130
names(counts)
## [1] "TP53"  "BRCA1" "EGFR"

3.4 Q15. Extract One Gene’s Count

counts["EGFR"]
## EGFR 
##  130

3.5 Q16. Extract the First Two Genes

counts[1:2]
##  TP53 BRCA1 
##   120   150

4 Section D — Managing Sample and Gene Metadata

4.1 Q17. Create Sample Names

samples <- c("Sample1", "Sample2", "Sample3")
samples
## [1] "Sample1" "Sample2" "Sample3"

4.2 Q18. Combine Gene Names with Counts

genes <- c("TP53", "BRCA1", "EGFR")
counts <- c(120, 150, 130)
names(counts) <- genes
counts
##  TP53 BRCA1  EGFR 
##   120   150   130

4.3 Q19. Check the Class of the Count Vector

class(counts)
## [1] "numeric"

5 Section E — Data Conversion and Filtering

5.1 Q20. Create Patient IDs with Sequence

patient_ids <- paste("Patient", 1:5, sep = "_")
patient_ids
## [1] "Patient_1" "Patient_2" "Patient_3" "Patient_4" "Patient_5"

5.2 Q21. What Happens in a Mixed Vector?

mixed_vector <- c("TP53", 100, TRUE)
mixed_vector
## [1] "TP53" "100"  "TRUE"
class(mixed_vector)
## [1] "character"
# All elements are coerced to character, since character is the
# most flexible type that can represent numbers, logicals, and text.

5.3 Q22. Convert a Character to Numeric

count_char <- "150"
count_numeric <- as.numeric(count_char)
count_numeric
## [1] 150
class(count_numeric)
## [1] "numeric"

5.4 Q23. Addition with Mixed Types (Fails)

x <- 5
y <- "6"
x + y
## Error in `x + y`:
## ! non-numeric argument to binary operator
# This fails with "non-numeric argument to binary operator" because
# y is a character string, not a number, and R cannot perform
# arithmetic between numeric and character types directly.

5.5 Q24. Fix the Error and Add

y <- as.numeric(y)
result <- x + y
result
## [1] 11

5.6 Q25. Filter Genes with High Expression

counts <- c(TP53 = 120, BRCA1 = 90, EGFR = 310)
high_expression_genes <- counts[counts > 100]
high_expression_genes
## TP53 EGFR 
##  120  310