Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Matrix

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

4.

2 Matrix

The Matrix in R is also called Matrices which is a two-dimensional data


structure that is used to store data in rows and columns. A row is a horizontal
representation of the elements and a column is a vertical representation of the
elements. All elements in the matrix should be of basic R type (integer,
character, boolean etc.).
Use the matrix() function to create a two-dimensional object in R
Programming Language. The following example creates a matrix with 3 rows
and 3 columns.
# Create matrix
data <- c(10,11,12,13,14,15,16,17,18)
mtx <- matrix(data,nrow=3,ncol=3,byrow=TRUE)
print(mtx)

# Get the type of data


print(class(mtx))

# Output:
# [,1] [,2] [,3]
# [1,] 10 11 12
# [2,] 13 14 15
# [3,] 16 17 18

# [1] "matrix" "array"


4.3 Array

In R, an array is a multi-dimensional data structure that can store values of the


same data type. It extends the concept of vectors (which are one-dimensional
arrays) to multiple dimensions. The array() function is used to create arrays
in R. For example, arr <- array(1:8, dim = c(2, 4))

# Create array
arr <- array(1:8, dim = c(2, 4))
print(arr)

# Get the type of data


print(class(arr))

# Output:
# [,1] [,2] [,3] [,4]
# [1,] 1 3 5 7
# [2,] 2 4 6 8

# [1] "matrix" "array"


Here, data to be stored in the array and dim is a vector specifying the
dimensions of the array.
4.4 List

A list in R acts as a versatile data structure that can store elements of different
data types. Unlike vectors or matrices, which can only store elements of the
same type, a list can contain elements of various types, including vectors,
matrices, data frames, other lists, and even functions. Lists are created using
the list() function. For example,

# Create list
my_list <- list(name = "John", age = 25, grades = c(90, 85, 92), is_student
= TRUE)
print(my_list)

# Get the type of data


print(class(arr))

# Output:
# $name
# [1] "John"

# $age
# [1] 25

# $grades
# [1] 90 85 92

# $is_student
# [1] TRUE

# [1] "list"
4.5 DataFrame

An R data frame represents the data in rows and columns similar to Python
pandas DataFrame and SQL. Each column in the data frame is a vector of the
same length, in other words, all columns in the data frame should have the
same length.
In the R data frame, columns are referred to as variables, and rows are
called observations. Refer to the R Data Frame Tutorial where I covered
several examples with explanations of working with data frames. In this R
programming tutorial section, I will just give you a glimpse look how the
DataFrame looks and how to create it.
You can create a data frame in R using the data.frame() function. A data
frame in R stores data in rows and columns, similar to tables in a relational
database management system (RDBMS). It is a two-dimensional data structure,
with one dimension representing rows and the other representing columns. I
will cover more of the data frame in the following sections.
#Create dataframe
my_dataframe =data.frame(
"id"=c(11,22,33,44,55),
"pages"=c(32,45,33,22,56),
"name"=c("spark","python","R","java","jsp"),
"chapters"=c(76,86,11,15,7),
"price"=c(144,553,321,567,890)
)

# Display the dataframe


print(my_dataframe )
Yields the output in a table.

You might also like