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›Data Types

Data Types

numeric, character, logical types; combining (paste0) and text↔number conversion.

Simple enough for a 5-year-old to understand

Basic types and paste0

You combine texts with paste0 (it doesn't put spaces in between).

Example code
1
name <- "Alex"
character (text).
Variablecharacter (text).
2
age <- 36
numeric (number).
Variablenumeric (number).
3
cat(paste0(name, " ", age, " years old"), "\n", sep="")
Alex is 36 years old
Output / InputAlex is 36 years old
VariableOutput / Input
💡 Hint: paste0 combines; paste puts spaces in between.

About this topic

R's fundamental data types are numeric, integer, character, logical, and complex. All of these are stored as vectors; a single value is simply a vector of length one.

When you type a number in R, the default type is numeric: the value 5 is numeric, not an integer. If you want an integer, you must append the letter L to the end. This distinction is often invisible, but it makes a difference in memory usage and the behavior of certain functions.

NA, which represents missing data, is the clearest trace of R's statistical origins. NA is not an error; it means "this value is unknown" and it propagates through calculations: the sum of a vector containing NA is also NA. This is intentional, because the result of a calculation involving an unknown value is also unknown.

What you will learn

  • Basic data types
  • The difference between integer and numeric
  • The concept of missing values
  • Automatic type conversion
  • Type checking functions

Who is this for?

  • Beginners working with data types for the first time
  • Those dealing with datasets containing missing data
  • Those encountering unexpected type coercion

When to use?

Missing values are inevitable in real datasets; any calculation performed without knowing how NA behaves carries the risk of incorrect results.

The factor type is used for categorical variables; leaving them as character strings will cause issues during the modeling phase.

Common mistakes

Wrong

degerler <- c(1, 2, NA, 4)
print(mean(degerler))

Because there is a missing value in the vector, the mean returns NA. This is not an error, but R's deliberate behavior: the average of a sum containing an unknown number is also unknown. You must explicitly specify that you want to ignore missing values.

Correct

degerler <- c(1, 2, NA, 4)
print(mean(degerler, na.rm = TRUE))

Wrong

karisik <- c(1, 2, "three")
print(karisik + 1)

A vector can hold a single type; when numbers and text are combined, R converts all of them to text, which is called coercion. The next line throws a "non-numeric argument to binary operator" error. The error is actually made on the line where the vector is created, not in the addition.

Correct

sayilar <- c(1, 2, 3)
print(sayilar + 1)

Wrong

x <- NA
if (x == NA) print("missing")

Comparing with NA returns NA, not TRUE or FALSE; because the condition cannot find a value, a "missing value where TRUE/FALSE needed" error is thrown. The logic is this: whether an unknown value is equal to another unknown value is also unknown. There is a separate function to test for missingness.

Correct

x <- NA
if (is.na(x)) print("missing")

Frequently asked questions

What is the difference between NA and NULL?

NA indicates that a value is missing, while NULL indicates that the value does not exist at all. NA takes up space in a vector and is included in the length; NULL does not remain as an element and disappears when added to a vector. Missing observations in datasets are NA.

Why should I write the number with L?

The L suffix specifies that the value is an integer. It saves memory in very large vectors and prevents type mismatches when some functions expect an integer. It is mostly unnecessary in daily analysis, but knowing the difference is useful for debugging.

What is the factor type used for?

It represents variables consisting of a limited number of categories: such as gender, province, group name. It stores the values internally as numbers and keeps labels separately. It is necessary for categorical variables to be processed correctly in statistical models.

Summary

The basic types are numeric, integer, character, and logical.

Numbers are floating-point by default; the L suffix is required for integers.

NA represents a missing value and propagates through calculations.

Different types in a vector are coerced into a single type.

Missingness is tested with is.na, not with equality.

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