May 12, 2021 R language tutorial
In general, when programming in any programming language, you need to use a variety of variables to store a variety of information. V ariables are simply where values are stored. T his means that when you create a variable, you have to keep some space in memory to store them.
You may want to store information about various data types, such as characters, wide characters, integers, floats, double floats, booleans, and so on. B ased on the data type of the variable, the operating system allocates memory and decides what can be stored in reserved memory.
In contrast to other programming languages, such as C and java in C, variables are not declared as a data type. T he variable is assigned an R object, and the data type of the R object becomes the data type of the variable. Although there are many types of R objects, they are often used as:
The simplest of these objects are vector objects, and these atomic vectors have six data types, also known as six types of vectors. T he other R objects are built on atomic vectors.
The data type | Cases | Check |
---|---|---|
Logic (logical) | TRUE, FALSE |
v <- TRUE print(class(v)) It produces the following results - [1] "logical" |
Numeric (Numbers) | 12.3,5,999 |
v <- 23.5 print(class(v)) It produces the following results - [1] "numeric" |
Integer (integer) | 2L,34L,0L |
v <- 2L print(class(v)) It produces the following results - [1] "integer" |
Complex (Composite) | 3 + 2i |
v <- 2+5i print(class(v)) It produces the following results - [1] "complex" |
Character (character) | 'a' , "good", "TRUE", '23.4' |
v <- "TRUE" print(class(v)) It produces the following results - [1] "character" |
Raw (Prototype) | "Hello" is stored as 48 65 6c 6c 6f |
v <- charToRaw("Hello") print(class(v)) It produces the following results - [1] "raw" |
In R programming, a very basic data type is an R object called a vector that holds elements of different classes as shown above. N ote that in R, the number of classes is not limited to the six types mentioned above. F or example, we can use many atomic vectors and create an array whose class becomes an array.
When you want to create vectors with multiple elements, you should use the c() function, which means combining elements into one vector.
# Create a vector. apple <- c('red','green',"yellow") print(apple) # Get the class of the vector. print(class(apple))
When we execute the code above, it produces the following results
[1] "red" "green" "yellow" [1] "character"
A list is an R object in which it can contain many different types of elements, such as vectors, functions, and even another list of them.
# Create a list. list1 <- list(c(2,5,3),21.3,sin) # Print the list. print(list1)
When we execute the code above, it produces the following results
[[1]] [1] 2 5 3 [[2]] [1] 21.3 [[3]] function (x) .Primitive("sin")
A matrix is a two-dimensional rectangular dataset. I t can be created using vector inputs for matrix functions.
# Create a matrix. M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE) print(M)
When we execute the code above, it produces the following results
[,1] [,2] [,3] [1,] "a" "a" "b" [2,] "c" "b" "a"
Although the matrix is limited to two dimensions, the array can have any number of dimensions. A rray functions use a dim property to create the required number of dimensionals. I n the following example, we create an array of two elements, each with 3x3 matrices.
# Create an array. a <- array(c('green','yellow'),dim = c(3,3,2)) print(a)
When we execute the code above, it produces the following results
, , 1 [,1] [,2] [,3] [1,] "green" "yellow" "green" [2,] "yellow" "green" "yellow" [3,] "green" "yellow" "green" , , 2 [,1] [,2] [,3] [1,] "yellow" "green" "yellow" [2,] "green" "yellow" "green" [3,] "yellow" "green" "yellow"
A factor is an r object created with a vector. I
t stores the vector as a label with different values of the elements in the vector. A
label is always a character, regardless of whether it is a number or a character or Boolean, etc. in the input vector.
They are useful in statistical modeling.
Create a factor using the factor() function.
The nlevels function gives a level count.
# Create a vector. apple_colors <- c('green','green','yellow','red','red','red','green') # Create a factor object. factor_apple <- factor(apple_colors) # Print the factor. print(factor_apple) print(nlevels(factor_apple))
When we execute the code above, it produces the following results
[1] green green yellow red red red green Levels: green red yellow # applying the nlevels function we can know the number of distinct values [1] 3
A data frame is a table data object. U
nlike the matrix in the data frame, each column can contain a different data pattern. T
he first column can be a number, the second column can be a character, and the third column can be logical.
It is a list of vectors of equal length.
Create
a data frame using the data.frame()
function.
# Create the data frame. BMI <- data.frame( gender = c("Male", "Male","Female"), height = c(152, 171.5, 165), weight = c(81,93, 78), Age = c(42,38,26) ) print(BMI)
When we execute the code above, it produces the following results
gender height weight Age 1 Male 152.0 81 42 2 Male 171.5 93 38 3 Female 165.0 78 26