89.98
[1] 89.98
55
[1] 55
Part 3: Data Types
William Okech
June 23, 2022
R and RStudio utilize multiple data types to store different kinds of data.
The most common data types in R are listed below.
Data Type | Description |
---|---|
Numeric | The most common data type. The values can be numbers or decimals (all real numbers). |
Integer | Special case of numeric data without decimals. |
Logical | Boolean data type with only 2 values (TRUE or FALSE ). |
Complex | Specifies imaginary values in R. |
Character | Assigns a character or string to a variable. The character variables are enclosed in single quotes (‘character’) while the string variables are enclosed in double quotes (“string”). |
Factor | Special type of character variable that represents a categorical such as gender. |
Raw | Specifies values as raw bytes. It uses built-in functions to convert between raw and character (charToRaw() or rawToChar()). |
Dates | Specifies the date variable. Date stores a date and POSIXct stores a date and time. The output is indicated as the number of days (Date) or number of seconds (POSIXct) since 01/01/1970. |
Several functions exist to examine the features of the various data types. These include:
typeof()
– what is the data type of the object (low-level)?class()
– what is the data type of the object (high-level)?length()
– how long is the object?attributes()
– any metadata available?Let’s look at how these functions work with a few examples
typeof()
class()
is.____()
functions to determine the data typeTo test whether the variable is of a specific type, we can use the is.____()
functions.
First, we test the variable a
which is numeric.
Second, we test the variable c
which is logical.
To convert between data types we can use the as.____()
functions. These include: as.Date()
, as.numeric()
, and as.factor()
. Additionally, other helpful functions include factor() which adds levels to the data and nchar()
which provides the length of the data.