5 Data Classes
There are various kinds of R-objects or data structures:
- Vectors
- Lists
- Matrices
- Arrays
- Factors
- Data Frames
Let’s first understand some of the basic datatypes on which the R-objects are built like Numeric, Integer, Character, Factor, and Logical.
5.2 Integer
Integer: Numbers that do not contain decimal values have a data type as an integer. However, to create an integer data type, you explicitly use as.integer() and pass the variable as an argument.
<- as.integer(2.2)
int print(int)
class(int)
5.3 Character
As the name suggests, it can be a letter or a combination of letters enclosed by quotes is considered as a character data type by R.
<- "datacamp"
char print(char)
class(char)
<- "12345"
char class(char)
5.4 Logical
Logical: A variable that can have a value of True and False like a boolean is called a logical variable.
<- TRUE
log_true print(log_true)
class(log_true)
<- FALSE
log_false class(log_false)
5.5 Factor
Factor: They are a data type that is used to refer to a qualitative relationship like colors, good & bad, course or movie ratings, etc. They are useful in statistical modeling.
<- factor(c("good", "bad", "ugly","good", "bad", "ugly"))
fac print(fac)
class(fac)
levels(fac)
nlevels(fac)
class(levels(fac))
5.6 List
Unlike vectors, a list can contain elements of various data types and is often known as an ordered collection of values. It can contain vectors, functions, matrices, and even another list inside it (nested-list).
<- seq(1,5) # Integer Vector
lis1 <- factor(1:5) # Factor Vector
lis2 <- letters[1:5]
lis3 <- list(lis1, lis2, lis3)
combined_list
1]]
combined_list[[2]]
combined_list[[3]]
combined_list[[3]][5]
combined_list[[
<- unlist(combined_list)
flat_list class(flat_list)
5.7 Vectors
Vectors are an object which is used to store multiple information or values of the same data type. A vector can not have a combination of both integer and character. For example, if you want to store 100 students’ total marks, instead of creating 100 different variables for each student, you would create a vector of length 100, which will store all the student marks in it.
<- c(88,65,90,40,65)
marks 4]
marks[1]
marks[6] marks[
5.7.1 Slicing vectors
seq(1,4)]
marks[c(1,2,4)] marks[
<- c("a", "b", "c")
char_vector class(char_vector)
length(char_vector)
1:3]
char_vector[-3]
char_vector[<- c(1,2, "a") char_num_vec
<- c("a", "b", "c")
char_vector class(char_vector)
length(char_vector)
1:3]
char_vector[-3] char_vector[
<- seq(1,1024)
vec <- c(1:1024) vec
5.8 Matrix
Similar to a vector, a matrix is used to store information about the same data type. However, unlike vectors, matrices are capable of holding two-dimensional information inside it.
<- matrix(vector,
M nrow=r,
ncol=c,
byrow=FALSE,
dimnames=list(char_vector_rownames, char_vector_colnames))
# byrow=TRUE signifies that the matrix should be filled by rows.
# byrow=FALSE indicates that the matrix should be filled by columns (the default).
<- matrix(seq(1,6),
M nrow = 2, ncol = 3, byrow = TRUE)
<- matrix(seq(1,6),
M nrow = 2, ncol = 3, byrow = F)
5.9 DataFrame
Unlike a matrix, Data frames are a more generalized form of a matrix. It contains data in a tabular fashion. The data in the data frame can be spread across various columns, having different data types.
<- data.frame(
dataset Person = c("Aditya", "Ayush","Akshay"),
Age = c(26, 26, 27),
Weight = c(81,85, 90),
Height = c(6,5.8,6.2),
Salary = c(50000, 80000, 100000))
class(dataset)
nrow(dataset)
ncol(dataset)
<- rbind(dataset, dataset)
df1 <- cbind(dataset, dataset)
df2 head(df1,3)
str(dataset)
summary(dataset)