Matrix
Matrix
Matrix
2 Matrix
# Output:
# [,1] [,2] [,3]
# [1,] 10 11 12
# [2,] 13 14 15
# [3,] 16 17 18
# Create array
arr <- array(1:8, dim = c(2, 4))
print(arr)
# Output:
# [,1] [,2] [,3] [,4]
# [1,] 1 3 5 7
# [2,] 2 4 6 8
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)
# 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)
)