Knowing how a programming language handles data is an important aspect one cannot miss. A data type is a particular classification of data that is understood by a programming language. You cannot disagree that 5 can be a number that we can add to 3 to obtain the number 8, but also it can be a character "5" when we describe the action of adding, as in: "Adding 5 to 3 we obtain 8."
*This guide assumes a basic knowledge of RStudio, for more on this program, click below:
R has five basic data types:
- Integer - 5,
- Double - 5.3,
- Character - a letter, a number (needs to be in quotation marks), a word, or a phrase,
- Logical - TRUE or FALSE,
- Complex - a complex number 5+3i, where i = sqrt(-1).
It is important to remember that R treats any numerical variable as double unless specified. Use typeof() function to determine the type of the argument according to R.
There are several functions in R that we can use to examine the object:
- typeof(),
- length(),
- class(),
- attributes(),
- summary(),
- str().
Try using those functions on your own to understand the language better.
Assign Single Values
In R, we use <- to assign value to the variable. Notice the change in the Environment window at the top right of RStudio, after you execute the assignment line k <- 5. The Global Environment of RStudio stores all the variables in a history file, saved in the working directory of the current R session.
k <- 5 # k = 5
typeof(k) # prints the type of the variable k
Output: [1] "double"
l <- 2.7 # l = 2.7
typeof(l) # prints the type of the variable l
Output: [1] "double"
To tell R to store the value as an integer, we can place the capital letter L just after the value itself. Since we have specified that k_integer, m, and n are integers, R treats them as integers.
k_integer <- 5L # specifying to treat k_integer as an integer 5 by placing L after 5
typeof(k_integer) # prints the type of the variable k_integer
Output: [1] "integer"
m <- n <- 1L # we can perform multiple assignments on the same line
typeof(m) # prints the type of the variable m
Output: [1] "integer"
Try the following codes and observe the output. Are those the data types you expected?
my_character <- "Hello World!" # variable my_character holds the string "Hello world!"
typeof(my_character) # prints the type of the variable my_character
Output: [1] "character"
length(my_character) # prints the of the object my_character
Output: [1] 1
Observe the output of the length (my_character) above. R treats everything as an object, and thus the result should be 1.
my_logical <- TRUE # variable my_logical holds a Boolean TRUE
typeof(my_logical) # prints the type of the variable my_logical
Output: [1] "logical"
my_complex <- 5+3i # variable my_complex holds a complex number 5+3i
typeof(my_complex) # print the type of the variable my_complex
Output: [1] "complex"
Assign Multiple Values
A function c() combines its arguments into a collection or bag of objects. Remember that a collection in the R programming language can contain objects of any data type.
x <- c(5, 2, 1) # collection x that holds numerical values of 5, 2, and 1
x # prints the values of x
Output: [1] 5 2 1
typeof(x) # prints the type of x
Output: [1] "double"
Recall our numeric variables k, l, m, and n from above, and let's create a collection y that contains all those variables. Check the Global Environment (the top right panel) variables in RStudio and ensure that those variables are listed. Otherwise, you receive an object not found error message, for example, "Error: object 'k' not found."
y <- c(k, l, m, n) # collection y that holds k, l, m, and n
y # prints the values of y
Output: [1] 5.0 2.7 1.0 1.0
typeof(y) # prints the type of y
Output: [1] "double"
class(y) # prints the class of y
Output: [1] "numeric"
In the example below, examine changes in the output and data type of the objects as we change the function c() arguments. R itself will guess the most appropriate type for the collection elements.
Method as.integer() tells R to treat its argument as an integer type.
y1 <- c(k, as.integer(l), m, n) # collection y1 that holds k, integer l, m, and n
y1 # prints the values of y1
Output: [1] 5 2 1 1
typeof(y1) # prints the type of y1
Output: [1] "double"
class(y1) # prints the class of y1
Output: [1] "numeric"
Now, let's examine the changes in the R output if we add a character object to the numeric collection y1. As you can see from the output below, the R programming language treats y2 as a character collection, printing each value in quotation marks.
y2 <- c(y1, my_character) # collection y2 that holds numeric elements of y1 and a character
y2 # prints the values of y2
Output: [1] "5" "2" "1" "1" "Hello World!"
typeof(y2) # prints the type of y2
Output: [1] "character"
class(y2) # prints the class of y2
Output: [1] "character"
Create Sequences
There are several ways to create a sequence of numbers. Please link to seq() in R documentation for a complete description of this useful function.
0:10 # prints values from 0 to 10
Output: [1] 0 1 2 3 4 5 6 7 8 9 10
10:0 # prints values from 10 to 0
Output: [1] 10 9 8 7 6 5 4 3 2 1 0
s <- 5:15 # creates a variable s that holds values from 5 to 15
s # prints the values of the object s
Output: [1] 5 6 7 8 9 10 11 12 13 14 15
seq(10) # prints values from 1 to 10
Output: [1] 1 2 3 4 5 6 7 8 9 10
seq(0, 10, by = 2) # prints every 2nd value from 0 to 10
Output: [1] 0 2 4 6 8 10
seq(10, 0, by = -2) # prints every 2nd value from 10 to 0
Output: [1] 10 8 6 4 2 0
Need More Help?
Click here to schedule a 1:1 with a tutor, coach, and or sign up for a workshop. *If this link does not bring you directly to our platform, please use our direct link to "Academic Support" from any Brightspace course at the top of the navigation bar.