R Types
-
Here we just introduce some basic "atomic" data types. There are many more, like
dates andtime series. -
Use function
is.x(), such asis.na()andis.complex(), to test the type of an R object.
| Type | Abbreviation |
|---|---|
| Integer | int |
| Numeric | dbl |
| Characters | chr |
| Logical | lgl |
| Factors | fctr |
| Date | date |
| Date-times | dttm |
Integer
Long-storage integers are written with L (otherwise they will be numerics)
class(5L) # "integer"
Numeric
A "numeric" is a double-precision floating-point number.
Scientific notations, infinities, and Not-a-Number are supported.
5e4
1.6e-35
Inf
class(-Inf)
class(NaN)
Complex
The imaginary part of a complex is appended with an i in R.
x <- 1 + 1i
y <- -1i + 1
x * y
class(x - 1i) # complex
Characters
There is no difference between strings and characters in R, unlike MATLAB.
And a string and a character are both char vectors of length 1.
Characters are surrounded by double quotes " (or single quotes ').
length("Hello")
length(c("Hello","World"))
Logical
Capital TRUE, FALSE, and NA are three logicals in R
TRUE # not true or True
class(NA)
FALSE == FALSE # TRUE
TandFare shorthands forTRUEandFALSE.
Factors
Factors are used to represent EDAV - Categorical Data and can be unordered or ordered. One can think of a factor as an integer vector where each integer has a label.
x <- factor(c("yes", "yes", "no", "yes", "no"))
x
# [1] yes yes no yes no
# Levels: no yes
## The "levels" are the values the categorical data can take
levels(x)
table(x)
# x
# no yes
# 2 3
## See the underlying representation of a factor
unclass(x)
# 2 2 1 2 1
# attr(x,"levels")
# [1] "no" "yes"
- Factor levels have an order
- Not like #Characters, which obey alphabetical order
- Factor levels serve as labels for data
- forcats is a useful package for manipulating factors
NULL
Capital NULL is a special empty object in R.
by zcysxy