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 the examples
Section titled “Run the examples”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.
Assignment
Section titled “Assignment”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 <- 1542gene_count[1] 1542sample_id <- "SAMPLE_01"sample_id[1] "SAMPLE_01"is_significant <- TRUEis_significant[1] TRUEVariable names in R are case sensitive. Join words with _ to keep names readable, and avoid starting a name with a number.
Numeric types
Section titled “Numeric types”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.45class(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 <- 15000Lclass(read_count)[1] "integer"Both types respond to is.numeric() as TRUE. But only the integer passes is.integer().
is.numeric(expression_value)[1] TRUEis.integer(read_count)[1] TRUECharacter
Section titled “Character”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] 5Logical
Section titled “Logical”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.003is_significant <- pvalue < 0.05is_significant[1] TRUECombine conditions with & for AND and | for OR. This is how you build multi-criteria filters for differential expression results.
log2fc <- 2.5passes_filter <- is_significant & abs(log2fc) > 1passes_filter[1] TRUEThis gene has a p-value below 0.05 and an absolute fold change above 1. It passes both filters.
NA values
Section titled “NA values”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 FALSECount the missing values:
sum(is.na(measurements))[1] 2Many 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] NAUse na.rm = TRUE to skip missing values in the calculation:
mean(measurements, na.rm = TRUE)[1] 4.775Always check your data for NA values before running statistical tests. Document how many values were missing and how you handled them.
Type checking
Section titled “Type checking”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"Type conversion
Section titled “Type conversion”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] 1600Convert 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] 3If R cannot convert a value, it returns NA and warns you:
as.numeric("not_a_number")[1] NAR 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.
Floating point: why == is the wrong test
Section titled “Floating point: why == is the wrong test”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.3format(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] TRUEDivision, remainder and rounding
Section titled “Division, remainder and rounding”The operators below perform division, integer division, and remainder in that order.
7 / 27 %/% 27 %% 2[1] 3.5[1] 3[1] 1R’s round() is banker’s rounding: halves go to the even number.
round(c(0.5, 1.5, 2.5))[1] 0 2 2This rule avoids always moving exact half cases upward.
1 is not an integer
Section titled “1 is not an integer”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.
Quick reference
Section titled “Quick reference”| 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 |
Next steps
Section titled “Next steps”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.
Further reading
Section titled “Further reading”-
The vector and missing-value sections explain type conversion, logical operations, and NA. These rules determine how the measurement vector behaves during arithmetic.
-
The reference distinguishes an object class from its underlying type. It explains why class and typeof can give different descriptions of the same object.
-
The reference defines numerical tolerance and the possible return values. It explains the need for isTRUE when a conditional expression requires one logical result.
-
The reference explains ties to even and binary representation of decimal values. Its warnings identify limits of decimal-half examples and platform behavior.