class(), typeof(), and
str() to inspect objectsNA,
NULL, NaN, and Inf, including
typed NAs%in%| Type | Example | Description |
|---|---|---|
numeric (double) |
3.14, 10 |
Any real number, decimal or whole; R’s default for numbers |
integer |
5L |
Whole numbers only (note the L suffix) |
character |
"hello" |
Text, always in quotes |
logical |
TRUE, FALSE |
Boolean values |
complex |
2+3i |
Numbers with an imaginary part |
num_var <- 3.14
int_var <- 5L
char_var <- "R is fun"
logical_var <- TRUE
complex_var <- 2 + 3i
class(num_var)## [1] "numeric"
## [1] "integer"
## [1] "character"
## [1] "logical"
## [1] "complex"
class()
vs typeof() vs str()class() tells you the object’s high-level type;
typeof() tells you its underlying storage mode;
str() gives a compact, human-readable structural summary —
arguably the single most useful inspection function in all of R.
## [1] "double"
## [1] "integer"
## num 3.14
## List of 3
## $ a: num 1
## $ b: chr "text"
## $ c: logi TRUE
0.1 + 0.2 != 0.3Like almost all programming languages, R stores decimal numbers using floating-point representation, which cannot represent every decimal value exactly.
## [1] FALSE
## [1] 0.3
## [1] TRUE
Best Practice: Never use
==to compare two decimal numbers directly. Useall.equal()(wrapped inisTRUE()) or round both sides to a sensible number of digits first.
greeting <- "Hello, World!"
apostrophe_example <- 'It\'s a sunny day' # escaping a single quote inside single quotes
newline_example <- "Line one\nLine two" # \n = newline
tab_example <- "Name:\tAmina" # \t = tab
cat(newline_example, "\n")## Line one
## Line two
## Name: Amina
| Escape Sequence | Meaning |
|---|---|
\n |
New line |
\t |
Tab |
\" |
Literal double quote inside a double-quoted string |
\\ |
A single literal backslash |
| Value | Meaning |
|---|---|
NA |
Missing / Not Available |
NULL |
Empty / absence of a value (zero length) |
NaN |
“Not a Number” — e.g. result of 0/0 |
Inf / -Inf |
Positive/negative infinity — e.g. 1/0 |
missing_value <- NA
empty_value <- NULL
not_a_number <- 0/0
infinite_value <- 1/0
is.na(missing_value)## [1] TRUE
## [1] TRUE
## [1] TRUE
## [1] TRUE
NAs (Often Overlooked)NA actually comes in several type-specific flavors under
the hood. This rarely matters for everyday use, but explains some
confusing edge cases you may encounter later:
## [1] "logical"
## [1] "integer"
## [1] "character"
## [1] "double"
NA
vs. NULL — A Critical Distinction## [1] 1
## [1] 0
## [1] 4
## [1] 1 2 4
Common mistake: Beginners often confuse
NA(a real but unknown/missing value that occupies a “slot”) withNULL(the complete absence of a value, zero length, and effectively invisible when combined into a vector).
R can convert values from one type to another using
as.*() functions.
## [1] "123"
## [1] 45.6
## [1] 7
## [1] -7
## [1] TRUE
## [1] 1
## [1] 0
Coercion that fails gracefully (produces NA with
a warning):
## Warning: NAs introduced by coercion
## [1] NA
When you combine different types together (e.g., in a vector, covered fully in Lesson 3), R automatically coerces everything to the “widest” compatible type, following this order:
logical -> integer ->
numeric -> character
## [1] 1 1
## [1] "1" "TRUE" "hello"
## [1] "character"
## [1] 1.0 2.5
## [1] "numeric"
## [1] 10
## [1] 4
## [1] 21
## [1] 2.333333
## [1] 1
## [1] 2
## [1] 343
These always return a logical
(TRUE/FALSE).
## [1] TRUE
## [1] FALSE
## [1] TRUE
## [1] TRUE
## [1] TRUE
Common mistake: Using a single
=to compare values instead of==. A single=is for assignment; using it inside anif()condition, for example, will usually throw a syntax error rather than silently doing the wrong thing — but it’s still a frequent source of confusion for beginners coming from other contexts.
## [1] FALSE
## [1] TRUE
## [1] FALSE
## [1] FALSE
## [1] TRUE
Rule of thumb: Use
&/|when working with whole vectors (e.g., filtering a data frame). Use&&/||only when comparing two single values, typically inside anif()condition — this will matter a great deal starting in Lesson 7.
Scenario: A university records a student’s exam score and pass threshold.
exam_score <- 76
pass_threshold <- 50
passed <- exam_score >= pass_threshold
cat("Score:", exam_score, "\n")## Score: 76
## Did the student pass? TRUE
## Score as a percentage of 100: 76 %
# A slightly larger, realistic example
student_majors <- c("CS", "Business", "Medicine")
applicant_major <- "Law"
cat("Is", applicant_major, "an offered major?", applicant_major %in% student_majors, "\n")## Is Law an offered major? FALSE
Simulate a very small piece of a university registration system:
full_name
(character), age (numeric), has_transcript
(logical), gpa (numeric).is.na() that would flag if
gpa were missing."twenty-one", then use as.numeric() on it and
observe what happens. Explain the result in a comment.%in% to check whether the applicant’s intended
major is in an approved list of 4 majors you define.& (e.g., age
above 18 AND has a transcript AND GPA above 2.0) into one final
eligible logical object.cat() that states
whether the applicant is eligible.age (numeric), your
name (character), and is_student (logical,
TRUE).class() and typeof() of each
object you just created.17 %% 5 and 17 %/% 5. Explain in a
comment what each result means."3.5" into a numeric value and
add 10 to it.as.numeric("R2D2")? Why?&,
|, and !. Predict the output before running
the code.is.na() to check whether NA == NA
returns what you expect. What did you discover?c(10, "20", TRUE) and
check its class(). Explain the coercion that happened.0.1 + 0.2 == 0.3 returns TRUE
or FALSE, and explain why using what you learned about
floating-point numbers.%in% to check whether the number 7 is
present in the vector c(2, 4, 6, 8, 10).NA and a vector containing
NULL in the same position; compare their
length().&& and explain, in
a comment, why & would also have worked (or not) in
that specific case.NA_character_ and NA_integer_
directly, and confirm their typeof() differs.as.numeric("abc")) and explain, in your own words,
what the warning message means.Q1. Which function reveals an object’s fundamental storage type in R?
class()typeof()str()format()Q2. What is the result of
as.integer(7.9)?
877.9Q3. What does NA represent in R?
Q4. Which operator checks whether two values are equal?
===!=<-Q5. When R combines c(1, TRUE, "hello")
into one vector, what type does the result become?
numericlogicalcharacterintegerQ6. Why does 0.1 + 0.2 == 0.3 return
FALSE in R?
0.3 is not a valid number in RQ7. What is the key difference between
NA and NULL?
NA represents a missing value and has a length of 1;
NULL represents total absence and has a length of 0NULL can only be used with numbersNA can only be used with textQ8. What does 5 %in% c(1, 3, 5, 7)
return?
TRUE5class(), typeof(), and
str() all help inspect them from different angles.==; use all.equal() for decimal
comparisons.NA, NULL, NaN, and
Inf are special values with distinct meanings;
NA has typed variants (NA_integer_,
NA_character_, etc.).as.*() functions) converts between types;
automatic coercion follows logical → integer → numeric → character.= vs ==,
& vs &&); %in% tests
membership.