0% found this document useful (0 votes)
0 views3 pages

( (1) ) (1) 2 5 3 ( (2) ) (1) 21 ( (3) ) (1) 3 ( (4) ) Function (X) .Primitive ("Sin")

The document provides an overview of various data structures in R, including lists, matrices, arrays, factors, and data frames, along with examples of how to create and print them. It also outlines arithmetic and relational operators used for mathematical operations and comparisons in R. Each data structure is described with its characteristics and uses in statistical modeling.

Uploaded by

medini bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

( (1) ) (1) 2 5 3 ( (2) ) (1) 21 ( (3) ) (1) 3 ( (4) ) Function (X) .Primitive ("Sin")

The document provides an overview of various data structures in R, including lists, matrices, arrays, factors, and data frames, along with examples of how to create and print them. It also outlines arithmetic and relational operators used for mathematical operations and comparisons in R. Each data structure is described with its characteristics and uses in statistical modeling.

Uploaded by

medini bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

A list is an R-object which can contain many different types of elements inside it like vectors,

functions and even other list inside it.


# create a list
List1 <- list( c(2,5,3),21,3,sin)
# print the list
Print(list1)

When we execute the above code we get the following result


[[1]]
[1] 2 5 3

[[2]]
[1] 21

[[3]]
[1] 3

[[4]]
function (x) .Primitive("sin")

Matrices in R
A matrix is a two dimensional rectangular data set. It can be created using a vector input to
the matrix function.

# create a matrix
M = matrix(c('a','a','b','c','b','a'),nrow = 3,byrow = TRUE)
print(M)

when we execute the above code we get:


[,1] [,2]
[1,] "a" "a"
[2,] "b" "c"
[3,] "b" "a"

Arrays in R

While matrices are confined to two dimensions, arrays can be of any number of dimensions.
The array function takes a dim attribute which creates the required number of dimensions.
In the below example we create an array with two elements which are 3*3 matrices each.

# create an array
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
if we run the code we get

[,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"

Factors in R
Factors are the r-objects which are created to using a vector.
It stores the vector along with the distinct values of the elements in the vector as the labels.
The labels are always character irrespective of whether it is numeric or character or Boolean etc, in
the input vector.
They are useful in statistical modelling.
Factors are created using the factor function.
The n levels function gives us the count of levels.

# create a vector
apple_colors <-
c('green','green','yellow','red','red','green')

# create the factor object


factor_apple <- factor(apple_colors)

# print the factor.


print(factor_apple)
print(nlevels(factor_apple))

after running the above code we get:


[1] green green yellow red red green
Levels: green red yellow
> print(nlevels(factor_apple))
[1] 3

Data frames in R
Data frames are tabular data objects.
Unlike in a matrix in data frame each column can contain different modes of data.
The first column can be a numeric while the second column can be character and third column can
be logical.
It is a list of vectors of equal length.
Data frames are created using the data frame function.
Data frame in R
# create the data frame
Arithmetic operators:
These operators are used to carry out mathematical operations like addition and multiplication.here
is a list of arithmetic operators available in R
Operator Description
+ addition
- Subtraction
 Multiplication
/ division

^ or ** exponentiation
X%%y modulus (x mod y)
X%/%y integer division

Relational operators:
Relational operators are used to compare between values. Here is a list of relational operators
available in R
< less than
 Greater than
<= less than or equal to
>= greater than or equal to
= equal to
!= not equal to

You might also like