Introduction to the R Programming Language 2025

Getting your Trinity Audio player ready...

Introduction to the R Programming Language

R Programming Language: If you work in the natural sciences, particularly in pharmacology or medicinal chemistry, you may have come across researchers utilizing R for data analysis. This article serves as an introduction to R, providing an overview of its features and benefits, even for those with no prior programming experience.

What is R? R Programming Language

R is a powerful programming language primarily used for statistical computing and data visualization. It is popular among statisticians, data scientists, and researchers in various scientific disciplines. R is complemented by RStudio, an integrated development environment (IDE) that provides a user-friendly interface for writing and executing R code. A significant advantage of R is that it is open-source and free to use, making it accessible to everyone.

Who Uses R?

R is widely used by professionals across various fields, including medicine, pharmacology, statistics, biology, and chemistry. Data scientists and researchers leverage R for tasks such as data analysis, visualization, and modeling. Many scientific studies and publications have utilized R, demonstrating its versatility. Examples of studies that have employed R include:

  • Chae, D. et al. (2018). Mechanistic Model for Blood Pressure and Heart Rate Changes Produced by Telmisartan in Human Beings. Basic & Clinical Pharmacology & Toxicology, 122(1), 139–148. DOI: 10.1111/bcpt.12856.
  • Hill, A. C. et al. (2019). Correction of Medication Nonadherence Results in Better Seizure Outcomes than Dose Escalation in a Novel Preclinical Epilepsy Model of Adherence. Epilepsia, 60(3), 475-484. DOI:10.1111/epi.14655.
  • Loder, A. L. et al. (2019). Water Chemistry of Managed Freshwater Wetlands on Marine-Derived Soils in Coastal Bay of Fundy, Canada. Wetlands, 39(3), 521–532. DOI: 10.1007/s13157-018-1101-y.

Installing R and RStudio

To begin using R, follow these steps:

  1. Download R from the Comprehensive R Archive Network (CRAN) and install it according to your operating system.
  2. Download and install RStudio to enhance your coding experience.
  3. Follow the installation instructions provided on the respective websites.

RStudio Environment Overview

Once installed, opening RStudio will display several key panels:

  • Code Editor Pane: Where you write your R scripts.
  • Console Pane: Where commands are executed immediately.
  • Workspace Pane: Displays environment variables, history, and connections.
  • Notebook Pane: Contains tabs for files, plots, packages, and help resources.

Customizing RStudio

You can personalize RStudio’s appearance by navigating to:

Tools > Global Options… > Appearance

Many users prefer themes like “Twilight” for better readability.

Running Code in RStudio

There are multiple ways to execute R code in RStudio:

  • Typing directly into the console for immediate execution.
  • Writing scripts in the Code Editor pane and running them with:
    • “Run” Button or Ctrl + Enter (Windows) for the current line.
    • “Source” Button or Ctrl + Shift + S (Windows) to execute the entire script.

Example:

print("Hello, R!")
cat("Welcome to R programming!\n")

Working with Variables in R

In R, variables store data values, which can be numeric or character-based. The <- operator is commonly used for assignment.

Example:

a_variable <- "Hello, World!"
another_variable <- 42
cat(a_variable, "\n", another_variable)

Vectors can store multiple values:

random_words <- c("apple", "banana", "cherry")
print(random_words)

Data Types in R

R supports various data types, including:

  • Numeric: Default data type for numbers.
  • Integer: Created using the suffix L (e.g., 5L).
  • Character: Enclosed in quotation marks (e.g., "text").
  • Logical: Boolean values (TRUE, FALSE).

Example:

num_value <- 3.14
int_value <- 5L
char_value <- "R Programming"
bool_value <- TRUE

cat("Numeric:", typeof(num_value), "\n")
cat("Integer:", typeof(int_value), "\n")
cat("Character:", typeof(char_value), "\n")
cat("Boolean:", typeof(bool_value), "\n")

Adding Comments in R

Comments help in understanding code better. In R, comments start with #.

Example:

# This is a comment
x <- 10  # Assigning 10 to x

Use Ctrl + Shift + C (Windows) to toggle comments.

Performing Calculations in R

R supports mathematical operations similar to Excel:

OperatorFunction
+Addition
-Subtraction
*Multiplication
/Division
^Exponentiation
sqrt()Square root
sum()Summation
mean()Mean calculation

Example:

a <- 10
b <- 5
cat("Sum:", a + b, "\n")
cat("Mean:", mean(c(a, b)))

Pharmaceutical Calculation Example

Calculate the molar mass of Enfuvirtide (C204H301N51O64):

C_val <- 12.01
H_val <- 1.01
N_val <- 14.01
O_val <- 16.00

enfuvirtide_val <- C_val*204 + H_val*301 + N_val*51 + O_val*64
cat("Molar Mass of Enfuvirtide [g/mol]:", enfuvirtide_val)

Learning More About R

To further your knowledge, explore these resources:

  • Online Courses: Udemy, Coursera, edX, DataCamp.
  • YouTube Tutorials: Barton Poulson’s Introduction to R.
  • Books:
    • A Beginner’s Guide to R by Zuur, Ieno & Meesters.
    • The R Book by Michael J. Crawley.

Additionally, use ?function_name in R to access built-in documentation.

By mastering R, you unlock the potential to perform advanced data analysis, statistical modeling, and visualization, making it an invaluable tool in scientific research and beyond!

Leave a Comment