Mini Med School 2024- R Tutorial

Author

Abisola Kehinde

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

# Addition
sum <- 2 + 3
print(sum)  # Outputs: 5
[1] 5

You can add options to executable code like this

# Load necessary packages 
# You can add your preferred data handling packages here
library(pacman)
pacman::p_load(pacman, dplyr, GGally, ggplot2, ggthemes, ggvis, httr, lubridate, plotly, rio, rmarkdown, shiny, stringr, tidyr)
library(data.table)

Attaching package: 'data.table'
The following objects are masked from 'package:lubridate':

    hour, isoweek, mday, minute, month, quarter, second, wday, week,
    yday, year
The following objects are masked from 'package:dplyr':

    between, first, last
library(readr)

# Step 1: Read the text file
file_path <- "/Users/abisolakehinde/Desktop/stripped_correlation_SERPINA1_1714302573_859.txt" # Specify the path to your text file
data <- read.table(file_path, header = TRUE, sep = "\t")

# Step 2: Filter the data based on correlation values
filtered_data <- data %>%
  filter(r.value >= 0.5 | r.value <= -0.5)

# Step 3: Sort the filtered data based on absolute correlation values in descending order
sorted_data <- filtered_data %>%
  arrange(-abs(r.value))


# Step 4: Get the top 20 genes
top_20_genes <- head(sorted_data, 20)


# Step 5: Write the top 20 genes to a new file
output_file_path <- "/Users/abisolakehinde/Desktop/top_correlated_genes.txt" # Specify the path to the output file
write.table(top_20_genes, file = output_file_path, sep = "\t", row.names = FALSE, col.names = TRUE, quote = FALSE)
# Print a message to confirm that the process is complete
cat("Top 20 genes have been written to", output_file_path)
Top 20 genes have been written to /Users/abisolakehinde/Desktop/top_correlated_genes.txt
# Step 6: Extract the bottom 20 rows from the sorted data
bottom_20_genes <- tail(sorted_data, 20)

# Append the bottom 20 genes to the existing file
write.table(bottom_20_genes, file = output_file_path, sep = "\t", row.names = FALSE, col.names = FALSE, quote = FALSE, append = TRUE)

# Confirm completion
cat("Bottom 20 genes have been appended to", output_file_path)
Bottom 20 genes have been appended to /Users/abisolakehinde/Desktop/top_correlated_genes.txt

The echo: false option disables the printing of code (only output is displayed).