Beginner

Variables & Data Types

Learn how R handles variables, assignment, data types, vectors, type conversion, and string operations.

Variables and Assignment

In R, you assign values to variables using the <- operator (preferred) or the = operator.

R
# Preferred assignment operator
x <- 42
name <- "R Programming"

# Also valid (but less idiomatic)
y = 100

# Right assignment (rarely used)
99 -> z

# Print variable values
x        # 42
print(y) # 100
Convention: The <- operator is the R community standard for assignment. Use = only for function arguments. In RStudio, press Alt+- to quickly type <-.

Data Types

R has several fundamental data types (also called "atomic types"):

R
# Numeric (double) - default for numbers
price <- 19.99
class(price)   # "numeric"
typeof(price)  # "double"

# Integer - append L to make it explicit
count <- 5L
class(count)   # "integer"

# Character (string)
greeting <- "Hello, R!"
class(greeting) # "character"

# Logical (boolean)
is_active <- TRUE
is_done <- FALSE
class(is_active) # "logical"

# Complex
z <- 3 + 4i
class(z)  # "complex"

# Special values
NA       # Missing value
NULL     # Empty/undefined
Inf      # Infinity
NaN      # Not a Number

Type Conversion

Convert between types using as.* functions:

R
# Convert to numeric
as.numeric("3.14")     # 3.14
as.numeric(TRUE)       # 1
as.numeric(FALSE)      # 0

# Convert to character
as.character(42)       # "42"
as.character(TRUE)     # "TRUE"

# Convert to integer
as.integer(3.7)        # 3 (truncates, does not round)

# Convert to logical
as.logical(0)          # FALSE
as.logical(1)          # TRUE
as.logical("yes")      # NA (cannot convert)

# Check types
is.numeric(42)         # TRUE
is.character("hello")  # TRUE
is.logical(TRUE)       # TRUE

Vectors

The most fundamental data structure in R is the vector. Even a single number is a vector of length 1. Create vectors with c():

R
# Create vectors with c()
numbers <- c(1, 2, 3, 4, 5)
names <- c("Alice", "Bob", "Charlie")
flags <- c(TRUE, FALSE, TRUE)

# Sequence shortcuts
1:10                    # 1, 2, 3, ..., 10
seq(0, 1, by = 0.2)    # 0.0, 0.2, 0.4, 0.6, 0.8, 1.0
rep(0, times = 5)      # 0, 0, 0, 0, 0

# Accessing elements (1-indexed!)
numbers[1]              # 1 (first element)
numbers[c(1, 3)]       # 1, 3
numbers[-2]             # All except 2nd element

# Vector operations (element-wise)
numbers * 2             # 2, 4, 6, 8, 10
numbers + 10            # 11, 12, 13, 14, 15
sum(numbers)             # 15
mean(numbers)            # 3
length(numbers)          # 5

String Operations

R
# Concatenate strings
paste("Hello", "World")          # "Hello World"
paste0("Hello", "World")         # "HelloWorld" (no separator)
paste("Item", 1:3, sep = "-")   # "Item-1" "Item-2" "Item-3"

# String length
nchar("R Programming")            # 13

# Substring
substr("Hello World", 1, 5)       # "Hello"

# Find and replace
gsub("old", "new", "old text")   # "new text"
sub("o", "0", "foo bar")        # "f0o bar" (first match only)

# Case conversion
toupper("hello")                 # "HELLO"
tolower("HELLO")                 # "hello"

# sprintf for formatted strings
sprintf("The value is %.2f", 3.14159)
# "The value is 3.14"

Math Operations

R
# Arithmetic
10 + 3    # 13
10 - 3    # 7
10 * 3    # 30
10 / 3    # 3.333333
10 %% 3   # 1 (modulo)
10 %/% 3  # 3 (integer division)
2 ^ 10    # 1024 (exponentiation)

# Math functions
sqrt(16)      # 4
abs(-7)       # 7
round(3.456, 2)  # 3.46
ceiling(3.2)   # 4
floor(3.8)     # 3
log(10)        # 2.302585 (natural log)
log10(100)     # 2
exp(1)         # 2.718282 (Euler's number)

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.