Numbers, text and logical values
Every vector has a type: number, text or logical value. How to recognise it, why a vector can have only one type and how to convert.
See first: Vectors
What you will learn
This lesson introduces the three basic data types of R, and afterwards you can:
- tell numbers (
numeric), text (character) and logical values (logical) apart and check the type withclass() - explain why a vector can have only one type and why R silently converts mixed values
- justify why logical values become numbers and numbers become text, never the other way round
- convert text into numbers with
as.numeric()and predict where this producesNA - recognise the error message when calculating with text and track down its cause with
class()
R distinguishes what kind of value a vector holds, because that decides what you can do with it. You can calculate with numbers, not with text. Three types are enough to begin with.
| Type | Example | For |
|---|---|---|
numeric |
1.09, 47 |
measurements, prices, counts |
character |
"vegan" |
text, such as categories and names |
logical |
TRUE, FALSE |
yes or no, the result of a comparison |
Text always goes in quotation marks. TRUE and FALSE go without, because
they are not text but logical values.
Asking for the type
class() tells you which type a value has:
diet <- c("vegan", "omnivore", "vegetarian")
class(diet)Whole numbers that R produces itself, for example with 1:10, have the type
integer. For you they behave like any other number.
One vector, one type
A vector can have only one type. If you mix numbers and text, R silently turns everything into text, because a number can always be written as text, but not every text as a number:
answers <- c(3, 5, "no answer", 2)
answersThe quotation marks in the output show it: the numbers have become text, which you can no longer calculate with. This happens often when reading data, when a single word sits in a column of numbers.
R does the same with numbers and logical values:
values <- c(5.4, TRUE, FALSE)
valuesTRUE has become 1 and FALSE 0. R always looks for the lowest common
denominator, the type into which all values can be converted. A logical
value can be read as 0 or 1, but what would 5.4 be as a logical value? That
is why the order is fixed: logical values become numbers and numbers become
text, never the other way round.
Converting
With as.numeric() you turn text back into numbers. Whatever cannot be read
as a number becomes NA, a missing value, and R warns you about it:
as.numeric(c("3", "5", "no answer", "2"))There is also as.character(). The other way round, R counts TRUE as 1
and FALSE as 0 when you calculate with logical values; you already used
that for counting with comparisons.
When R is to calculate with text
If you try to calculate with text, R stops and says that an argument is not numeric:
"3" + 4When you meet this message, check with class() which type your values
really have.
Key points
- Three types to begin with: numbers (
numeric), text (character) and logical values (logical);class()tells you the type. - A vector has only one type; mixed values become text.
as.numeric()converts and turns anything unreadable intoNA.
Details
- Level
- Beginner
- Modules
- Empirical Research
- In learning paths
- Getting started with R
- Code
7019- Updated
- 1 November 2026
- Status
- published