HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RVariablesData TypesOperatorsCharacters (character)VectorsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ Character
📚 Vector
🔀 Condition & Loop
🧩 Function
R›Variables

Variables

Assigning values with <-; types are inferred from the value, and values can be changed.

Simple enough for a 5-year-old to understand

Assignment: <-

In R, assignment is done with <- (= also works, but <- is traditional).

Example code
1
name <- "Alex"
String variable.
VariableString variable.
2
age <- 36
Numeric variable.
VariableNumeric variable.
3
cat(name, "\n", sep="")
Alex
Output / InputAlex
4
cat(age, "\n", sep="")
36
Output / Input36
VariableOutput / Input
💡 Hint: The <- arrow puts the value on the right into the box on the left.

About this topic

In R, the arrow operator is used to define variables. You do not declare the variable type; the value you assign determines the type, and you are free to assign a value of a completely different type later.

In R, there is actually no pure scalar. When you write x <- 5, you are creating a single-element vector. This is the core design decision of the language and explains why vector operations feel so natural.

R is a bit relaxed with naming rules: names can start with a letter or a dot, and can contain dots. Since a dot means member access in many languages, seeing lots of dots in R code feels strange at first, but here it is just a name character.

What you will learn

  • Variable definition
  • The difference between assignment operators
  • Naming conventions
  • Deleting and listing variables
  • That a single value is actually a vector

Who is this for?

  • Beginners to R
  • Those coming from other languages who want to get used to the assignment syntax
  • Those who want to keep their workspace organized

When to use?

Storing every intermediate result that you will reuse throughout the analysis in a variable makes the code both readable and reproducible.

Keeping unnecessary copies in variables during large datasets fills up the memory; deleting objects that are no longer needed is a good habit.

Common mistakes

Wrong

2sayi <- 10

A variable name cannot start with a number; R gives an "unexpected symbol" error. Names must start with a letter or a dot, and if they start with a dot, the next character must not be a number. This rule is common in almost all languages.

Correct

sayi2 <- 10

Wrong

toplam <- 10
Toplam <- Toplam + 5

Since R is case-sensitive, Toplam and toplam are different variables; the second line tries to read an undefined object and gives an "object 'Toplam' not found" error. Choosing a consistent naming convention prevents this error.

Correct

toplam <- 10
toplam <- toplam + 5

Wrong

T <- 0
if (T) print("worked")

T and F are abbreviations for TRUE and FALSE but are not reserved: values can be assigned to them. After such an assignment, T no longer means true, and conditions will silently fail. Therefore, it is recommended to always explicitly write TRUE and FALSE in your code.

Correct

durum <- FALSE
if (TRUE) print("worked")

Frequently asked questions

Is it safe to use a dot in a variable name?

It is valid in R and frequently seen in older code. However, since the dot can carry special meanings in R's object system, it is safer to use an underscore in new code. Modern style guides recommend underscore-separated naming.

How do I see the variables I have defined?

The ls() function lists the names of the objects in the workspace. rm() is used to delete an object, and rm(list = ls()) is used to clear all of them. Starting an analysis with a clean environment prevents values left over from previous work from affecting the results.

How do I find out the type of a variable?

class() gives the class of the object, while typeof() gives the internal storage mode in memory. The two do not always return the same result; for example, the class of an integer vector is integer, while the class of a date object might be Date and its storage mode double.

Summary

Assignment is done with the arrow operator.

Names cannot start with a number.

The language is case-sensitive.

T and F can be overwritten, TRUE and FALSE should be preferred.

Even a single value is actually a vector.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code