R Data Structures, Functions, and Packages, Data Manipulation and Visualization in R

  • R is a widely used programming language for statistical computing, data analysis, and visualization in bioinformatics. Biological research frequently involves large datasets containing gene-expression measurements, DNA sequences, protein abundances, genetic variants, clinical observations, and experimental metadata. To analyze these datasets effectively, researchers need to understand how R stores information, how functions perform calculations, how packages extend its capabilities, and how data can be cleaned, transformed, summarized, and visualized. These skills form the foundation for statistical bioinformatics workflows, including differential gene-expression analysis, genomic data exploration, and the interpretation of biological experiments.
  • R provides built-in data structures for representing different types of information, functions for carrying out calculations and repetitive tasks, and a package ecosystem that supports specialized scientific applications. Its data manipulation and visualization tools make it possible to convert raw experimental measurements into organized datasets, perform statistical analyses, identify patterns, and communicate results through informative graphics. In bioinformatics, these capabilities are particularly valuable because biological datasets are often multidimensional, contain missing values, and require careful integration with sample annotations and experimental designs.

R Data Structures

  • Data structures determine how information is stored, organized, accessed, and processed in R. Selecting an appropriate structure improves code readability, computational efficiency, and the reliability of subsequent analyses. The principal data structures include vectors, matrices, arrays, lists, data frames, and factors. Each structure has particular characteristics that make it suitable for different types of biological information.
  • A vector is one of the most fundamental data structures in R. It stores multiple elements of the same basic data type, such as numeric values, integers, logical values, or character strings. Vectors are useful for storing gene-expression measurements, nucleotide counts, protein concentrations, sample identifiers, and statistical results. The c() function combines values into a vector, while functions such as length(), sum(), mean(), and sd() provide information about its contents.

gene_expression <- c(5.2, 6.1, 4.8, 7.0, 5.7)

mean(gene_expression)

sd(gene_expression)

length(gene_expression)

  • R supports vectorized operations, meaning that many calculations can be applied to entire vectors without explicitly writing a loop. For example, multiplying a vector of measurements by two doubles every element. Comparisons can also be applied to vectors to identify observations that meet specified conditions.

expression <- c(3.2, 5.6, 7.8, 4.1, 9.2)

expression * 2

expression > 5

expression[expression > 5]

  • Vectorization is useful in biological data processing because it allows researchers to perform calculations across thousands of measurements using concise expressions. However, vectors generally contain elements of a common type. If different types are combined, R may coerce them into a common type, potentially changing how the data are interpreted.
  • A matrix is a two-dimensional structure containing elements of the same basic type, organized into rows and columns. Matrices are particularly useful for representing gene-expression data, where rows may represent genes and columns may represent biological samples. They also support linear algebra operations used in statistical modeling, dimensionality reduction, and other computational analyses.

expression_matrix <- matrix(

  c(5.2, 6.1, 4.8, 7.0, 5.7, 6.4),

  nrow = 3,

  ncol = 2

)

rownames(expression_matrix) <- c(“GeneA”, “GeneB”, “GeneC”)

colnames(expression_matrix) <- c(“Sample1”, “Sample2”)

print(expression_matrix)

  • Matrices can be indexed by row and column positions, allowing researchers to extract individual measurements, genes, or samples. Unlike a typical data frame, a matrix stores all elements using a common underlying type. Consequently, matrices are appropriate for numerical measurements but are less convenient when a dataset contains a mixture of numerical values, categorical variables, and textual metadata.
  • An array extends the matrix concept to three or more dimensions. Arrays are useful when biological measurements have multiple dimensions, such as genes, samples, experimental conditions, and time points. For example, a three-dimensional array might represent gene-expression measurements across multiple tissues and treatments. The array() function creates such structures by specifying the data and dimensions.

biological_array <- array(

  1:24,

  dim = c(4, 3, 2)

)

dim(biological_array)

biological_array[, , 1]

  • A list is a flexible data structure that can contain objects of different types and sizes. A list may hold a numerical vector, a character string, a matrix, a statistical model, or another list. This flexibility makes lists valuable for storing the outputs of bioinformatics analyses, which often include measurements, metadata, statistical summaries, and graphical objects.

analysis_result <- list(

  gene = “GeneA”,

  expression = c(5.2, 6.1, 7.0),

  mean_expression = 6.1,

  significant = TRUE

)

analysis_resultgeneanalysisresultexpression

analysis_result$significant

  • Lists are widely used by R functions because a single function can return several related results in one object. For example, a statistical model may return estimated coefficients, residuals, fitted values, and diagnostic information. Understanding list indexing and the $ operator is therefore important when working with statistical and bioinformatics packages.
  • A data frame is one of the most useful structures for biological data analysis. It organizes observations into rows and variables into columns. Different columns may contain different data types, such as numerical measurements, gene identifiers, sample names, treatment groups, and organism labels. This makes data frames particularly suitable for storing experimental results and metadata together.

gene_data <- data.frame(

  Gene = c(“GeneA”, “GeneB”, “GeneC”, “GeneD”),

  Expression = c(5.2, 6.1, 4.8, 7.0),

  Treatment = c(“Control”, “Control”, “Treated”, “Treated”)

)

print(gene_data)

str(gene_data)

summary(gene_data)

  • Data frames can be inspected using head(), tail(), str(), summary(), and dim(). Researchers can extract columns using $ or bracket notation and select rows using logical conditions. Data frames are convenient for descriptive statistics, group comparisons, data cleaning, and visualization. For more complex genomic datasets, specialized Bioconductor classes may be more appropriate because they can preserve genomic coordinates, assay measurements, and biological annotations in a structured form.
  • Factors represent categorical variables and are especially important in statistical analysis. Examples include treatment groups, disease status, genotype categories, tissue types, and experimental batches. A factor stores categories as levels and can preserve their intended order.

treatment <- factor(

  c(“Control”, “Treated”, “Control”, “Treated”),

  levels = c(“Control”, “Treated”)

)

levels(treatment)

  • Factors help R distinguish categorical variables from numerical measurements and can influence how regression models, analysis of variance, and graphical functions interpret the data. Ordered factors can represent categories with a meaningful ranking, such as mild, moderate, and severe. Researchers should check factor levels carefully because unexpected reference categories or incorrectly ordered levels can affect model interpretation.

Functions in R

  • Functions are reusable blocks of code that perform a defined task. They accept inputs, carry out operations, and may return outputs. Functions reduce repetition, improve organization, and make analytical workflows easier to test and maintain. R includes many built-in functions, and researchers can create their own functions to automate biological calculations.
  • Common built-in functions include mean(), median(), sum(), min(), max(), range(), length(), sort(), and unique(). These functions are useful for summarizing biological measurements, inspecting identifiers, and identifying distinct categories.

protein_abundance <- c(12.5, 15.2, 13.8, 18.1, 14.7)

mean(protein_abundance)

median(protein_abundance)

range(protein_abundance)

sort(protein_abundance)

  • A user-defined function is created with the function() keyword. The function can accept one or more arguments and return a calculated result. For example, GC content is a common sequence characteristic in DNA analysis. The following function calculates the percentage of G and C bases in a DNA sequence while excluding ambiguous characters from the denominator.

calculate_gc <- function(sequence) {

  sequence <- toupper(gsub(“\\s+”, “”, sequence))

  bases <- strsplit(sequence, “”)[[1]]

  valid_bases <- bases[bases %in% c(“A”, “T”, “G”, “C”)]

  if (length(valid_bases) == 0) {

    return(NA_real_)

  }

  gc_count <- sum(valid_bases %in% c(“G”, “C”))

  100 * gc_count / length(valid_bases)

}

calculate_gc(“ATGCGCGCATATGC”)

  • This function illustrates how a biological calculation can be converted into a reusable tool. It removes whitespace, standardizes letter case, identifies standard DNA bases, and returns the GC percentage. Excluding ambiguous characters is one possible convention; alternative analyses may use different rules depending on the research question. Input validation can be expanded to detect invalid symbols or report warnings when sequences contain unexpected characters.
  • Functions can also accept default arguments, making them more flexible. For example, a function that summarizes a numerical variable might allow users to choose between the mean and median. A well-designed function should have a clear purpose, meaningful argument names, appropriate output, and predictable behavior for missing or invalid inputs.

summarize_measurements <- function(x, method = “mean”) {

  if (method == “mean”) {

    return(mean(x, na.rm = TRUE))

  } else if (method == “median”) {

    return(median(x, na.rm = TRUE))

  } else {

    stop(“Method must be ‘mean’ or ‘median’.”)

  }

}

summarize_measurements(c(2, 4, 6, 8))

summarize_measurements(c(2, 4, 6, 8), method = “median”)

  • The argument na.rm = TRUE tells the functions to ignore missing values for these summaries. Missing-value handling should nevertheless be deliberate: removing missing observations can affect estimates if the missingness is related to biological characteristics or experimental conditions. The stop() function generates an error when an unsupported method is requested, helping prevent silent mistakes.
  • Functions are particularly useful when the same analysis must be applied to multiple genes, samples, tissues, or experimental conditions. A researcher may create a function that calculates mean expression, standard deviation, and sample size for a group, then apply it consistently across hundreds of genes. Reusable functions also make it easier to validate analytical procedures and maintain reproducible workflows.

Packages in R

  • A package is a collection of functions, datasets, documentation, and sometimes compiled code that extends R’s capabilities. R packages support a wide range of tasks, from general statistical analysis to specialized bioinformatics applications. The CRAN repository hosts packages for data manipulation, visualization, statistical modeling, and many other fields. Bioconductor provides a specialized ecosystem for genomic, transcriptomic, proteomic, and other high-throughput biological analyses.
  • A package is generally installed once in an R environment and loaded when required. The install.packages() function installs packages from CRAN, while library() loads an installed package for use in the current R session.

install.packages(“ggplot2”)

library(ggplot2)

  • Installation and loading are separate operations. Package installation may require an internet connection and suitable system dependencies, while loading makes the package’s functions available in the current session. Researchers should avoid reinstalling packages unnecessarily within every analysis script and should record package versions for reproducibility.
  • Several packages are particularly useful for biological data manipulation and visualization. dplyr provides functions for filtering, selecting, arranging, transforming, and summarizing data. tidyr helps convert datasets between wide and long formats. readr supports importing tabular data, while data.table provides efficient operations for large datasets. ggplot2 supports layered statistical graphics, and pheatmap can create heatmaps of numerical matrices. Other packages support statistical modeling, survival analysis, network analysis, and machine learning.
  • Bioconductor packages are particularly important for specialized bioinformatics tasks. DESeq2 and edgeR support differential expression analysis of RNA sequencing count data; limma supports a variety of statistical analyses, including microarray analysis and many other expression-data applications. GenomicRanges provides data structures and methods for genomic intervals, while Biostrings supports biological sequence manipulation. These tools are designed for different types of data and analytical questions, so the appropriate package should be selected according to the experiment and its assumptions.
  • Bioconductor packages are typically installed through the BiocManager package. For example:

install.packages(“BiocManager”)

BiocManager::install(“Biostrings”)

  • A package’s documentation should be consulted before applying it to research data. Researchers need to understand expected input formats, normalization requirements, statistical assumptions, and output interpretation. For example, RNA sequencing differential expression methods generally expect raw count data and a suitable experimental design rather than arbitrary transformed values. Applying a package to incompatible data can produce misleading results even if the code runs successfully.
  • Package management is also important for reproducible research. Package updates can introduce changes in functions, dependencies, or numerical results. Researchers can inspect installed versions using packageVersion() and use project-management tools such as renv to record dependencies. Keeping analytical scripts, data-processing steps, and package versions together helps ensure that results can be repeated and independently evaluated.

Data Manipulation in R

  • Data manipulation involves organizing, filtering, cleaning, combining, transforming, and summarizing information before statistical analysis. In bioinformatics, raw datasets often require extensive preparation because they may contain thousands of genes, multiple biological samples, experimental metadata, missing measurements, and identifiers from different sources. Careful manipulation helps ensure that the data are consistent and appropriate for statistical modeling.
  • Data can be imported using functions such as read.csv() and read.delim(). After importing a dataset, researchers should inspect its dimensions, column names, data types, missing values, and representative rows. For large datasets, efficient import tools may be preferable.

data <- read.csv(“gene_expression.csv”)

dim(data)

names(data)

head(data)

str(data)

summary(data)

  • These checks help identify whether numerical columns have been imported as character strings, whether identifiers have been interpreted correctly, and whether the dataset contains missing or unexpected values. A reliable workflow should verify that the imported data match the source file and that the number of rows and columns is consistent with expectations.
  • Selecting columns and filtering rows are common manipulation tasks. Base R supports these operations through bracket indexing and logical expressions. The dplyr package provides the functions select() and filter() for similar purposes.

library(dplyr)

selected_data <- data %>%

  select(Gene, Expression, Treatment)

high_expression <- data %>%

  filter(Expression > 5)

  • The pipe operator %>% passes the result of one operation into the next, allowing multiple steps to be written as a readable sequence. Recent versions of R also provide the native pipe operator |>. Filtering can be used to select samples meeting specified criteria, remove records that fail predefined quality checks, or focus on genes with particular characteristics. Thresholds should be chosen according to scientific justification rather than selected solely to produce a preferred result.
  • New variables can be created using mutate(). In biological studies, derived variables may include log-transformed measurements, normalized values, ratios, or indicators of experimental categories. For example:

data <- data %>%

  mutate(

    log_expression = log2(Expression + 1)

  )

  • The addition of one prevents taking the logarithm of zero in this particular example, but the transformation should only be used when appropriate for the measurement scale. A log transformation can reduce right-skewness and make multiplicative differences easier to examine, but it is not automatically suitable for all biological data. For RNA sequencing, specialized normalization and transformation methods should generally be used instead of treating raw counts as ordinary continuous measurements.
  • Grouping and summarization help researchers calculate statistics across experimental conditions. The group_by() and summarise() functions in dplyr can summarize observations by treatment, tissue, genotype, or another categorical variable.

group_summary <- data %>%

  group_by(Treatment) %>%

  summarise(

    mean_expression = mean(Expression, na.rm = TRUE),

    sd_expression = sd(Expression, na.rm = TRUE),

    sample_count = sum(!is.na(Expression)),

    .groups = “drop”

  )

print(group_summary)

  • This example calculates the mean, standard deviation, and number of nonmissing observations for each treatment group. Such summaries provide a useful overview before formal statistical testing. Researchers should distinguish biological replicates from technical replicates and ensure that the unit of analysis matches the experimental design.
  • Datasets often need to be combined. For example, a gene-expression table may need to be joined with a separate annotation table containing gene names, descriptions, or functional categories. The merge() function in base R and the joining functions in dplyr, such as left_join(), can combine datasets using shared identifiers.

expression_data <- data.frame(

  GeneID = c(“G001”, “G002”, “G003”),

  Expression = c(10.2, 4.8, 8.6)

)

annotation_data <- data.frame(

  GeneID = c(“G001”, “G002”, “G003”),

  GeneName = c(“GeneA”, “GeneB”, “GeneC”)

)

combined_data <- left_join(

  expression_data,

  annotation_data,

  by = “GeneID”

)

print(combined_data)

  • Joining datasets requires careful attention to identifier consistency and uniqueness. If a key appears multiple times in either table, a join may produce additional rows and change the structure of the data. Researchers should check for unmatched identifiers, duplicated keys, and differences in gene nomenclature or annotation versions before interpreting the merged dataset.
  • Missing values are represented by NA in R. They can be detected with is.na() and summarized using functions such as sum(is.na(data$Expression)). Depending on the analysis, missing values may require investigation, filtering, imputation, or specialized statistical methods. Missingness can occur because of measurement failure, low abundance, technical limitations, or biological characteristics. Automatically replacing missing values with zero is often inappropriate because zero and missing represent different states.
  • Reshaping data is another important operation. Wide-format data often place each sample or condition in a separate column, while long-format data store each observation in a separate row with columns describing the sample, condition, and measurement. The pivot_longer() and pivot_wider() functions from tidyr facilitate these transformations.

library(tidyr)

wide_data <- data.frame(

  Gene = c(“GeneA”, “GeneB”),

  Control = c(5.2, 3.8),

  Treated = c(7.1, 4.6)

)

long_data <- pivot_longer(

  wide_data,

  cols = c(Control, Treated),

  names_to = “Treatment”,

  values_to = “Expression”

)

print(long_data)

  • Long-format data are often convenient for plotting with ggplot2, grouping observations, and fitting models. Wide-format matrices may be more appropriate for certain matrix-based analyses or specialized bioinformatics packages. The choice of format should be guided by the analytical task and the requirements of the functions being used.

Data Visualization in R

  • Data visualization is essential for exploring biological measurements, evaluating data quality, identifying trends, and communicating findings. Numerical summaries alone may not reveal outliers, skewed distributions, sample clustering, batch effects, or nonlinear relationships. Graphical methods help researchers recognize these patterns and decide which statistical analyses are appropriate.
  • R provides a built-in graphics system as well as specialized visualization packages. Base R supports histograms, scatter plots, box plots, bar plots, and line charts. The ggplot2 package is widely used because it provides a flexible system for constructing layered graphics using data, aesthetic mappings, geometric objects, scales, and themes. Biological researchers use these capabilities to visualize gene-expression measurements, genomic variation, protein abundance, clinical outcomes, and relationships between biological variables.
  • A histogram displays the distribution of a numerical variable by grouping observations into intervals. It is useful for examining expression values, protein concentrations, sequencing quality measurements, or other continuous data.

expression <- c(2.1, 3.4, 3.8, 4.0, 4.2, 4.7, 5.1, 5.8, 6.2)

hist(

  expression,

  main = “Distribution of Gene Expression”,

  xlab = “Expression level”,

  ylab = “Frequency”

)

  • The shape of a histogram can reveal whether measurements are approximately symmetric, skewed, multimodal, or influenced by extreme observations. Its appearance depends on the selected bin widths, so researchers should avoid interpreting small changes in shape as strong evidence of biological differences.
  • A scatter plot displays the relationship between two numerical variables. It can be used to examine correlations between gene-expression measurements, compare experimental methods, or investigate associations between gene expression and a biological trait.

gene_a <- c(2.1, 3.5, 4.0, 5.2, 6.1)

gene_b <- c(2.5, 3.2, 4.3, 5.0, 6.5)

plot(

  gene_a,

  gene_b,

  main = “Relationship Between Two Genes”,

  xlab = “Gene A expression”,

  ylab = “Gene B expression”,

  pch = 19

)

  • A scatter plot can reveal positive or negative associations, clusters, nonlinear patterns, and unusual observations. However, a visual relationship does not establish causation. Correlation coefficients, regression models, and appropriate uncertainty estimates may be required to quantify the association.
  • Box plots summarize distributions using the median, quartiles, and potential outliers. They are useful for comparing gene-expression measurements between experimental groups or examining variation among tissues and treatments.

expression <- c(4.2, 4.5, 4.8, 5.0, 5.2, 6.1, 6.4, 6.8)

group <- factor(

  c(“Control”, “Control”, “Control”, “Control”,

    “Treated”, “Treated”, “Treated”, “Treated”)

)

boxplot(

  expression ~ group,

  main = “Gene Expression by Treatment”,

  xlab = “Treatment group”,

  ylab = “Expression level”

)

  • Box plots can highlight differences in central tendency and spread, but they do not show every observation unless individual points are added. When sample sizes are small, displaying the raw measurements alongside summary statistics can improve transparency.
  • The ggplot2 package provides a consistent approach to creating more elaborate visualizations. Its ggplot() function establishes the dataset and aesthetic mappings, while layers such as geom_point(), geom_boxplot(), geom_line(), and geom_histogram() determine how observations are displayed.

library(ggplot2)

plot_data <- data.frame(

  Treatment = factor(

    c(“Control”, “Control”, “Control”,

      “Treated”, “Treated”, “Treated”),

    levels = c(“Control”, “Treated”)

  ),

  Expression = c(4.8, 5.1, 5.4, 6.2, 6.5, 6.8)

)

ggplot(plot_data, aes(x = Treatment, y = Expression)) +

  geom_boxplot() +

  geom_jitter(width = 0.08, height = 0) +

  labs(

    title = “Gene Expression Across Treatment Groups”,

    x = “Treatment”,

    y = “Expression level”

  ) +

  theme_minimal()

  • This figure combines a box plot with individual observations, allowing readers to examine both the distribution summary and the measured values. The use of labs() provides meaningful labels, while theme_minimal() applies a simplified graphical style. Good visualization practice also includes clear units, readable text, accessible colors, and an appropriate scale.
  • Heatmaps are particularly important in bioinformatics because they can display measurements for many genes and samples simultaneously. Rows often represent genes, columns represent samples, and colors indicate expression levels or other measurements. Heatmaps may reveal groups of genes with similar expression patterns, clusters of samples, and differences between experimental conditions.
  • A simple heatmap can be produced using a numerical matrix and the heatmap() function. Specialized packages, such as pheatmap, offer additional control over clustering, annotations, and presentation.

expression_matrix <- matrix(

  c(

    5.1, 5.5, 8.2, 8.5,

    3.2, 3.4, 6.1, 6.4,

    7.0, 7.2, 4.1, 4.3

  ),

  nrow = 3,

  byrow = TRUE

)

rownames(expression_matrix) <- c(“GeneA”, “GeneB”, “GeneC”)

colnames(expression_matrix) <- c(

  “Control1”, “Control2”, “Treated1”, “Treated2”

)

heatmap(

  expression_matrix,

  scale = “row”,

  main = “Gene Expression Heatmap”

)

  • In this example, scale = “row” standardizes the values within each gene, emphasizing relative patterns across samples rather than absolute expression differences between genes. This can be useful when genes have very different expression ranges, but it changes the interpretation of the color scale. Researchers should state whether values are raw, normalized, transformed, or standardized and should use methods appropriate to the underlying assay.
  • Principal component analysis is another common application of visualization in statistical bioinformatics. PCA reduces the dimensionality of multivariate data by identifying directions that capture major sources of variation. A PCA plot can help researchers assess similarities among samples, identify potential outliers, and investigate whether experimental groups separate along major axes of variation. It can also reveal patterns associated with technical factors such as sequencing batches or processing dates. PCA is exploratory, and separation between groups does not by itself establish statistical significance or prove that the treatment caused the observed differences.
  • Other specialized plots include volcano plots, which display estimated effect sizes against statistical significance in differential-expression studies; Manhattan plots, which show genome-wide association results across genomic positions; survival curves, which display time-to-event outcomes; and network diagrams, which represent relationships among genes, proteins, or other biological entities. These visualizations often require additional packages and careful interpretation of the statistical quantities displayed.

Integrating Data Manipulation, Statistics, and Visualization

  • The main strength of R is that data manipulation, statistical analysis, and visualization can be combined into a coherent workflow. A typical bioinformatics analysis begins by importing raw measurements and metadata, checking data quality, transforming the data where appropriate, calculating descriptive summaries, performing statistical analyses, and generating figures. Each step should be documented so that the results can be reproduced and reviewed.
  • For example, a researcher studying a gene-expression experiment might import a table containing gene identifiers and measurements from control and treated samples. The researcher could inspect missing values, summarize expression by group, visualize the distributions, and then use an appropriate statistical model to compare conditions. The resulting estimates, confidence intervals, p-values, and adjusted p-values could be combined with gene annotations to identify candidate biological processes for further investigation.
  • However, a general data-manipulation workflow should not replace specialized statistical methods where they are needed. RNA sequencing count data, for example, require appropriate normalization and models that account for count distributions and biological variability. Gene-level tests should address multiple comparisons, and the experimental design should account for factors such as pairing, batch effects, and covariates. Plots should reflect the actual statistical quantities used in the analysis rather than presenting transformed values as though they were raw measurements.
  • Reproducibility is central to this workflow. Analytical code should be saved in scripts or notebooks, and data sources, package versions, parameter choices, and processing decisions should be documented. Random procedures can often be made reproducible with set.seed(), while tools such as R Markdown and Quarto can combine code, results, graphics, and explanatory text in a single report. Version-control systems such as Git can help researchers track changes and collaborate on analytical projects.
  • In conclusion, understanding R data structures, functions, and packages provides the foundation for effective statistical bioinformatics. Vectors, matrices, arrays, lists, data frames, and factors organize biological information, while functions make analytical procedures reusable and packages extend R with specialized statistical and computational capabilities. Data manipulation tools help researchers clean, filter, transform, combine, and summarize measurements, while visualization methods reveal patterns, variability, and relationships that may not be apparent from tables alone. By integrating these skills with appropriate statistical methods, biological knowledge, and reproducible practices, researchers can develop reliable workflows for genomics, transcriptomics, proteomics, and other areas of computational biology.
Author:

Leave a Reply

Your email address will not be published. Required fields are marked *