Data Types in R
There are five basic data types in R:
- Numeric — This is the default treatment for numbers. This data type includes integers and doubles.
- Double — A double allows you to store numbers as decimals. This is the default treatment for numbers.
- Integer — An integer is a subset of the numeric data type. This type will only allow whole numbers and is denoted by the letter “L”.
2. Complex — This type is created by using the imaginary variable “i”.
3. Character — This type is used for storing non-numeric text data.
4. Logical — Sometimes referred to as “boolean”, this data type will store either “TRUE” or “FALSE”.
5. Raw — Used less often, this data type will store data as raw bytes.
Numeric
Double
Let’s explore the “double” data type by assigning a number to a variable and then check it’s type by using the “typeof” function. Alternatively, we can use the “is.double” function to check whether or not the variable is a double.
x <- 6.2
typeof(x)
# [1] "double"
is.double(x)
# [1] TRUE
Next, let’s check whether or not the variable is numeric by using the “is.numeric” function.
is.numeric(x)
# [1] TRUE
This function should return “TRUE” as well, which demonstrates the fact that a double is a subset of the numeric data type.
Integer
Let’s explore the “integer” data type by assigning a whole number followed by the capital letter “L” to a variable and then check it’s type by using the “typeof” function. Alternatively, we can use the “is.integer” function to check whether or not the variable is an integer.
x <- 6L
# By using the "typeof" function, we can check the data type of x
typeof(x)
# [1] "integer"
is.integer(x)
# [1] TRUE
Next, let’s check whether or not the variable is numeric by using the “is.numeric” function.
is.numeric(x)
# [1] TRUE
This function should return “TRUE” as well, which demonstrates the fact that an integer is also a subset of the numeric data type.
Complex
Complex data types make use of the mathematical concept of an imaginary number through the use of the lowercase letter “i”. The following example sets “x” equal to six times i and then displays the type of x.
x <- 6i
typeof(x)
# [1] "complex"
Character
Character data types store text data. When creating characters, make sure you wrap your text in quotation marks.
x <- "Hello!"
typeof(x)
# [1] "character"
Logical
Logical data types store either “TRUE” or “FALSE”. Unlike characters, these data should not be wrapped in quotation marks.
x <- TRUE
typeof(x)
# [1] "logical"
Raw
Used less often, the raw data type will store data as raw bytes. You can convert character data types to raw data types by using the “charToRaw” function. Similarly, you can convert integer data types to raw data types through the use of the “intToBits” function.
x <- charToRaw("Hello!")
print(x)
# [1] 48 65 6c 6c 6f 21
typeof(x)
# [1] "raw"
x <- intToBits(6L)
print(x)
# [1] 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# [26] 00 00 00 00 00 00 00
typeof(x)
# [1] "raw"
This is an excerpt from “R for Data Analysis” . You can read the full book here: https://trevorfrench.github.io/R-for-Data-Analysis/