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:
Let's briefly look at each of these.
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
> # Assign a decimal value to x > > x <- 8.5 > > # Print the value of x > x  8.5 > > # Print the class name of x > > class(x)  "numeric" >
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)  "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)  "integer" > > > # It is an integer now >
In R, a complex value is defined using a pure imaginary value
> #Assign a complex value to a variable > x = 1 + 2i > > #Print the complex value x > x  1+2i > > #Print the class of x > class(x)  "complex" >
Logical values are boolen values (
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  FALSE > > #Print the class of z > class(z)  "logical" >
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  "John Doe" >
If we have an object of some other data type (e.g., numeric), we can convert it into character type using the
> #Create a numeric variable > > x <- 8.75 > > #Print the variable x > > x  8.75 > > #Print the class of x > > class(x)  "numeric" > > #convert x into a character value > > y <- as.character(x) > > #Print the variable y > > y  "8.75" > > #Print the class of y > > class(y)  "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  FALSE > x | y # Checks that either x OR y is true  TRUE > !x # Returns the negation of x  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)  "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)  "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)  "GOOG" > > #Replace the first occurance of a word in a string by another word. > > sub("John","Sam","His first name is John")  "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:
#>  "I go for a walk at 7:05 a.m."
See the special syntax
% 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.
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).
Please login to view this lesson.
With our free registration, you can access to all the lessons on finance, risk, data analytics and data science for finance professionals.Sign in free