Skip to content

Variables & Data Types

Every analysis in R starts with storing values. Gene names, read counts, p-values, and sample identifiers all need to live somewhere. Variables give your data a name. Data types determine what R can do with that data. Understanding both is essential before you can write useful bioinformatics code.

Run these blocks in an R session, in page order. An R console shows a > prompt; your shell terminal runs commands such as ls and is a different environment. Copy the R code without the output blocks underneath it. Those blocks show what the R console prints, including its [1] index labels.

Save the code in an R script so you can repeat the lesson in a fresh session. When you use an editor, send each block from that script to the R console. The examples on this page use base R. Later lessons explain packages and container environments.

R uses the <- operator to assign values to variables. You can also use =, but <- is the standard R convention. You will see it in nearly all R code and documentation.

Store a gene count, a sample identifier, and a significance flag:

gene_count <- 1542
gene_count
[1] 1542
sample_id <- "SAMPLE_01"
sample_id
[1] "SAMPLE_01"
is_significant <- TRUE
is_significant
[1] TRUE

Variable names in R are case sensitive. Join words with _ to keep names readable, and avoid starting a name with a number.

Most numbers in bioinformatics fall into two categories. Expression values, fold changes, and p-values are decimals. Read counts and gene counts are whole numbers. R handles these differently.

By default, R treats numbers as numeric, which stores them as double precision floating point values. This is the right type for expression levels and statistical results.

expression_value <- 23.45
class(expression_value)
[1] "numeric"

For whole number counts, add the L suffix to create an integer. Integers use less memory. This matters when you work with millions of read counts.

read_count <- 15000L
class(read_count)
[1] "integer"

Both types respond to is.numeric() as TRUE. But only the integer passes is.integer().

is.numeric(expression_value)
[1] TRUE
is.integer(read_count)
[1] TRUE

Text data shows up constantly in bioinformatics. Gene names, sample IDs, chromosome labels, and file paths are all character strings. Wrap text in quotes to create a character value.

gene_name <- "BRCA1"
chromosome <- "chr17"
paste("Gene", gene_name, "is on", chromosome)
[1] "Gene BRCA1 is on chr17"

The paste() function joins strings together. The nchar() function counts the number of characters. Both are useful when parsing gene names or sequence data.

nchar(gene_name)
[1] 5

Logical values are either TRUE or FALSE. They drive filtering and decision making in your analysis. Every time you ask “is this gene significant?” or “does this sample pass QC?”, you get a logical result.

Compare a p-value to a threshold:

pvalue <- 0.003
is_significant <- pvalue < 0.05
is_significant
[1] TRUE

Combine conditions with & for AND and | for OR. This is how you build multi-criteria filters for differential expression results.

log2fc <- 2.5
passes_filter <- is_significant & abs(log2fc) > 1
passes_filter
[1] TRUE

This gene has a p-value below 0.05 and an absolute fold change above 1. It passes both filters.

Real biological data has missing values. A failed assay, a dropped sample, or a below-detection measurement all produce gaps in your data. R represents missing data as NA.

Knowing how to detect and handle NA values is critical. One unhandled NA can silently break your calculations.

measurements <- c(5.2, 3.1, NA, 4.8, NA, 6.0)
is.na(measurements)
[1] FALSE FALSE TRUE FALSE TRUE FALSE

Count the missing values:

sum(is.na(measurements))
[1] 2

Many R functions return NA when the input contains any missing values. This is a safety feature, not a bug. It forces you to decide how to handle the gaps.

mean(measurements)
[1] NA

Use na.rm = TRUE to skip missing values in the calculation:

mean(measurements, na.rm = TRUE)
[1] 4.775

Always check your data for NA values before running statistical tests. Document how many values were missing and how you handled them.

Use class() to inspect the class that affects method selection. A class is not the same as an internal type. Use typeof() for the internal type; a numeric matrix, for example, can have matrix class and double type.

class(gene_name)
[1] "character"
class(is_significant)
[1] "logical"
class(42)
[1] "numeric"
class(42L)
[1] "integer"

Sometimes you need to convert between types. A read count stored as text in a CSV file needs to become a number before you can do math with it. R provides as.* functions for this.

count_string <- "1500"
count_number <- as.numeric(count_string)
count_number + 100
[1] 1600

Convert a number to a character string:

as.character(42)
[1] "42"

Converting a decimal to an integer truncates the decimal portion. R does not round.

as.integer(3.7)
[1] 3

If R cannot convert a value, it returns NA and warns you:

as.numeric("not_a_number")
[1] NA

R also writes a coercion warning to its error stream. The captured value above comes from standard output. When this warning appears during an import, inspect the column for unexpected text before treating the result as a missing value.

A double is a binary approximation. 0.1 has no exact binary form, so arithmetic on it does not land where you expect, and R hides this by printing only seven digits.

0.1 + 0.2 == 0.3
format(0.1 + 0.2, digits = 20)
[1] FALSE
[1] "0.30000000000000004441"

Use all.equal() to compare within a tolerance. It returns TRUE when the comparison succeeds and a character description when it finds a difference. That description is not a logical condition for if. Wrap the result in isTRUE() to obtain one logical value, as the R documentation recommends.

isTRUE(all.equal(0.1 + 0.2, 0.3))
[1] TRUE

The operators below perform division, integer division, and remainder in that order.

7 / 2
7 %/% 2
7 %% 2
[1] 3.5
[1] 3
[1] 1

R’s round() is banker’s rounding: halves go to the even number.

round(c(0.5, 1.5, 2.5))
[1] 0 2 2

This rule avoids always moving exact half cases upward.

class(1)
class(1L)
[1] "numeric"
[1] "integer"

This matters when a function demands an integer, and when you are counting on exact equality for whole numbers.

Type Example Created with Common use in bioinformatics
numeric 23.45 Direct assignment Expression values, p-values, fold changes
integer 15000L L suffix Read counts, gene counts
character "BRCA1" Quotes Gene names, sample IDs, chromosome labels
logical TRUE Comparison operators Significance flags, QC pass/fail
NA NA Missing data Failed assays, below detection limit

You now know how to store and inspect individual values. Most bioinformatics work involves collections of values. Learn how to group data into vectors, lists, and data frames in Data Structures.

  • An Introduction to R

    The vector and missing-value sections explain type conversion, logical operations, and NA. These rules determine how the measurement vector behaves during arithmetic.

  • R class reference

    The reference distinguishes an object class from its underlying type. It explains why class and typeof can give different descriptions of the same object.

  • R all.equal reference

    The reference defines numerical tolerance and the possible return values. It explains the need for isTRUE when a conditional expression requires one logical result.

  • R round reference

    The reference explains ties to even and binary representation of decimal values. Its warnings identify limits of decimal-half examples and platform behavior.