Data Types in R
In R programming, there are five basic data types. Also, everything in R is an object, so we will refer to these data types as classes of objects. So, these five types of objects are:
- Numeric
- Integer
- Complex
- Logical
- Character
Let's briefly look at each of these.
Numeric
In R, decimal values such as 8.5
are numeric. You can assign a numeric value to a variable and then that variable will be of numeric data type.
For any variable or object, you can check it's data type by using the class
command.
> # Assign a decimal value to x
>
> x <- 8.5
>
> # Print the value of x
> x
[1] 8.5
>
> # Print the class name of x
>
> class(x)
[1] "numeric"
>
Integer
Integers are the natural numbers such as 1, 2, 3, etc. In R, when you assign a number to a variable, it is by default of the numeric class type. In order to create an integer variable, we use a function called as.integer()
. This ensures that the variable created is indeed an integer.
> #Assign a value to a variable
> x <- 8
>
> #check the class type of x
> class(x)
[1] "numeric"
>
> # You will notice that it is numeric.
>
> # Use the as.integer() function to create an integer
>
> y <- as.integer(8)
>
> #check the class type of yx
> class(y)
[1] "integer"
>
> > # It is an integer now
>
Complex
In R, a complex value is defined using a pure imaginary value <em>i</em>
.
> #Assign a complex value to a variable
> x = 1 + 2i
>
> #Print the complex value x
> x
[1] 1+2i
>
> #Print the class of x
> class(x)
[1] "complex"
>
Logical
Logical values are boolen values (TRUE
or FALSE
) often created by comparison between variables.
> #Create sample variables
> x <- 5
> y <- 7
>
> #Check if y is less than x
> z <- y < x
>
> # z will have a logical value. Let's print it
>
> z
[1] FALSE
>
> #Print the class of z
> class(z)
[1] "logical"
>
Character
Character values are string or text values. We can create a character type variable by assigning a string to the variable. Notice the use of double quotes.
> #Create a character variable
>
> x <-"John Doe"
>
> #Print the character string
>
> x
[1] "John Doe"
>
If we have an object of some other data type (e.g., numeric), we can convert it into character type using the as.character()
function.
> #Create a numeric variable
>
> x <- 8.75
>
> #Print the variable x
>
> x
[1] 8.75
>
> #Print the class of x
>
> class(x)
[1] "numeric"
>
> #convert x into a character value
>
> y <- as.character(x)
>
> #Print the variable y
>
> y
[1] "8.75"
>
> #Print the class of y
>
> class(y)
[1] "character"
>
Standard Logical Operations
We can perform standard logical operations such as "&" (and), "|" (or), and "!" (negation) on logical values in R.
> x = TRUE; y = FALSE
> x & y # Checks that both x AND y are true
[1] FALSE
> x | y # Checks that either x OR y is true
[1] TRUE
> !x # Returns the negation of x
[1] FALSE
>
Standard String Operations
We can easily perform basic string operations in R such as concatenation, or creating readable strings.
> #Define sample character variables
> fname <- "John"
> lname <- 'Doe'
>
> # Concatenate strings using paste function
> paste(fname, lname)
[1] "John Doe"
>
> #Create a readable string using sprintf() function
> #Use %s for strings and %d for numeric values
>
> sprintf("His first name is %s and last name is %s",fname,lname)
[1] "His first name is John and last name is Doe"
>
> # Extract a substr from a long string. In the following example, we
> # are extracting stock symbol from text
>
> substr("Google's stock symbol is GOOG", start=26, stop=29)
[1] "GOOG"
>
> #Replace the first occurance of a word in a string by another word.
>
> sub("John","Sam","His first name is John")
[1] "His first name is Sam"
>
The sprintf() function allows you to create strings as output using formatted data. For example:
sprintf("I go for a walk at %s:%s%s a.m.", 7, 0, 5)
This will output:
#> [1] "I go for a walk at 7:05 a.m."
See the special syntax %s
. Each %
is referred to as a slot, which is basically a placeholder for a variable that will be formatted. The values after the comma are the variables or values that will be filled in each slot.
Getting Help
We can find many more functions for these data types in the R documentation. For each function, the help also provides details about how to use these functions. For example, you can get help for sprintf()
function by using the command help('sprintf')
. The documentation will open in the help window (bottom right).
Data Science in Finance: 9-Book Bundle
Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.
What's Included:
- Getting Started with R
- R Programming for Data Science
- Data Visualization with R
- Financial Time Series Analysis with R
- Quantitative Trading Strategies with R
- Derivatives with R
- Credit Risk Modelling With R
- Python for Data Science
- Machine Learning in Finance using Python
Each book includes PDFs, explanations, instructions, data files, and R code for all examples.
Get the Bundle for $39 (Regular $57)Free Guides - Getting Started with R and Python
Enter your name and email address below and we will email you the guides for R programming and Python.