0% found this document useful (0 votes)
3 views30 pages

R Programming

The document provides an overview of data types in R programming, including vectors, lists, matrices, arrays, factors, and data frames, along with their creation and usage examples. It also discusses package installation and loading methods, as well as various operators such as arithmetic, relational, logical, and assignment operators in R. Each section includes syntax and example outputs to illustrate the concepts effectively.

Uploaded by

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

R Programming

The document provides an overview of data types in R programming, including vectors, lists, matrices, arrays, factors, and data frames, along with their creation and usage examples. It also discusses package installation and loading methods, as well as various operators such as arithmetic, relational, logical, and assignment operators in R. Each section includes syntax and example outputs to illustrate the concepts effectively.

Uploaded by

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

Data types in R programming

There are many types of R-objects. The frequently used ones are −
 Vectors
 Lists
 Matrices
 Arrays
 Factors
 Data Frames
The simplest of these objects is the vector object and there are six data
types of these atomic vectors, also termed as six classes of vectors. The
other R-Objects are built upon the atomic vectors.

Data Type Example Verify

Logical TRUE, FALSE Live Demo


v <- TRUE
print(class(v))
it produces the following
result −
[1] "logical"

Numeric 12.3, 5, 999 Live Demo


v <- 23.5
print(class(v))
it produces the following
result −
[1] "numeric"

Integer 2L, 34L, 0L Live Demo


v <- 2L
print(class(v))
it produces the following
result −
[1] "integer"

Complex 3 + 2i Live Demo


v <- 2+5i
print(class(v))
it produces the following
result −
[1] "complex"

Character 'a' , '"good", "TRUE", Live Demo


'23.4' v <- "TRUE"
print(class(v))
it produces the following
result −
[1] "character"

Raw "Hello" is stored as 48 65 Live Demo


6c 6c 6f v <- charToRaw("Hello")
print(class(v))
it produces the following
result −
[1] "raw"

Vectors
When you want to create vector with more than one element, you should
use c() function which means to combine the elements into a vector.
Live Demo
# Create a vector.
apple <- c('red','green',"yellow")
print(apple)

# Get the class of the vector.


print(class(apple))
When we execute the above code, it produces the following result −
[1] "red" "green" "yellow"
[1] "character"

Lists
A list is an R-object which can contain many different types of elements
inside it like vectors, functions and even another list inside it.
Live Demo
# Create a list.
list1 <- list(c(2,5,3),21.3,sin)

# Print the list.


print(list1)
When we execute the above code, it produces the following result −
[[1]]
[1] 2 5 3

[[2]]
[1] 21.3

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

Matrices
A matrix is a two-dimensional rectangular data set. It can be created
using a vector input to the matrix function.
Live Demo
# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)
When we execute the above code, it produces the following result −
[,1] [,2] [,3]
[1,] "a" "a" "b"
[2,] "c" "b" "a"

Arrays
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 dimension. In the below example we
create an array with two elements which are 3x3 matrices each.
Live Demo
# Create an array.
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
When we execute the above code, it produces the following result −
,,1

[,1] [,2] [,3]


[1,] "green" "yellow" "green"
[2,] "yellow" "green" "yellow"
[3,] "green" "yellow" "green"

Factors
Factors are created using the factor() function. The nlevels functions
gives the count of levels.
Live Demo
# 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 above code, it produces the following result −
[1] green green yellow red red red green
Levels: green red yellow
[1] 3

Data Frames
Data frames are tabular data objects. Unlike a matrix in data frame each
column can contain different modes of data. The first column can be
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.
Live Demo
# 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 above code, it produces the following result −
gender height weight Age
1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26

R PACKAGE
The most common method of installing and loading packages is using
the install.packages() and library() function respectively. Let us see a brief about
these functions –
 Install.packages() is used to install a required package in the R programming
language.

Syntax:
install.packages(“package_name”)
 library() is used to load a specific package in R programming language

Syntax:
library(package_name)
In the case where multiple packages have to installed and loaded these commands
have to be specified repetitively. Thus making the approach inefficient.

install.packages("ggplot2")
install.packages("dpylr")
install.packages("readxl")

library(ggplot2)
library(dpylr)
library(readxl)

The most efficient way to install the R packages is by installing multiple packages at a
time using. For installing multiple packages we need to use install.packages( ) function
again but this time we can pass the packages to be installed as a vector or a list with
each package separated by comma(,).
Syntax :
install.packages ( c(“package 1″ ,”package 2”, . . . . , “package n”) )
install.packages(“package1″,”package2”, . . . . , “package n”)
Example :

install.packages(c("ggplot2","dpylr","readxl"))

install.packages("ggplot2","dpylr","readxl")

Similarly, package can be loaded efficiently by one of the following ways.


Method 1: Using library()
In this, the packages to be loaded are passed to the function but as a list with each
package separated by a comma (,).
Syntax:
library(“package1”, “package2″, . . . . ,”package n”)
Example:

library("ggplot2","dpylr")

Method 2: Using pacman


For efficient package loading, we need to install another package called
pacman. To load multiple packages using pacman we use a function
called p_load( ).
Syntax :
pacman::p_load( package 1 , . . . . , package n)
Example :

pacman::p_load(dplyr,ggplot2,readxl)

Operators in R
There are the following types of operators used in R:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Miscellaneous Operators

Arithmetic Operators
Arithmetic operators are the symbols which are used to represent arithmetic
math operations. The operators act on each and every element of the vector.
There are various arithmetic operators which are supported by R

S. Operator Description Example


No

1. + This operator is used to b <- c(11, 5, 3)


print(a+b)
add two vectors in R. a
<- c(2, 3.3, 4)
It will give us the following output:

[1] 13.0 8.3 5.0

2. - This operator is used to b <- c(11, 5, 3)


print(a-b)
divide a vector from
another one. a <- c(2, It will give us the following output:
3.3, 4)
[1] -9.0 -1.7 3.0

3. * This operator is used to b <- c(11, 5, 3)


print(a*b)
multiply two vectors with
each other. a <- c(2, 3.3, It will give us the following output:
4)
[1] 22.0 16.5 4.0

4. / This operator divides the b <- c(11, 5, 3)


print(a/b)
vector from another one.
It will give us the following output:
a <- c(2, 3.3, 4)
[1] 0.1818182 0.6600000
4.0000000

5. %% This operator is used to b <- c(11, 5, 3)


print(a%%b)
find the remainder of the
first vector with the It will give us the following output:
second vector. a <- c(2,
3.3, 4) [1] 2.0 3.3 0

6. %/% This operator is used to a <- c(2, 3.3, 4)


b <- c(11, 5, 3)
find the division of the
print(a%/%b)
first vector with the
second(quotient). It will give us the following output:

[1] 0 0 4

7. ^ This operator raised the b <- c(11, 5, 3)


print(a^b)
first vector to the
exponent of the second It will give us the following output:
vector. a <- c(2, 3.3, 4)
[1] 0248.0000 391.3539 4.0000
Relational Operators
A relational operator is a symbol which defines some kind of relation
between two entities. These include numerical equalities and inequalities. A
relational operator compares each element of the first vector with the
corresponding element of the second vector. The result of the comparison
will be a Boolean value. There are the following relational operators which
are supported by R:

S. Operator Description Example


No

1. > This operator will return TRUE a <- c(1, 3, 5)


b <- c(2, 4, 6)
when every element in the first
print(a>b)
vector is greater than the
corresponding element of the It will give us the following
second vector. output:

[1] FALSE FALSE FALSE

2. < This operator will return TRUE a <- c(1, 9, 5)


b <- c(2, 4, 6)
when every element in the first
print(a<b)
vector is less then the
corresponding element of the It will give us the following
second vector. output:

[1] FALSE TRUE FALSE

3. <= This operator will return TRUE a <- c(1, 3, 5)


b <- c(2, 3, 6)
when every element in the first
print(a<=b)
vector is less than or equal to the
corresponding element of another It will give us the following
vector. output:

[1] TRUE TRUE TRUE

4. >= This operator will return TRUE a <- c(1, 3, 5)


b <- c(2, 3, 6)
when every element in the first
print(a>=b)
vector is greater than or equal to
the corresponding element of It will give us the following
another vector. output:

[1] FALSE TRUE FALSE

5. == This operator will return TRUE a <- c(1, 3, 5)


b <- c(2, 3, 6)
when every element in the first
print(a==b)
vector is equal to the
corresponding element of the It will give us the following
second vector. output:

[1] FALSE TRUE FALSE

6. != This operator will return TRUE a <- c(1, 3, 5)


b <- c(2, 3, 6)
when every element in the first
print(a>=b)
vector is not equal to the
corresponding element of the It will give us the following
second vector. output:

[1] TRUE FALSE TRUE

Logical Operators
The logical operators allow a program to make a decision on the basis of
multiple conditions. In the program, each operand is considered as a
condition which can be evaluated to a false or true value. The value of the
conditions is used to determine the overall value of the op1 operator op2.
Logical operators are applicable to those vectors whose type is logical,
numeric, or complex.
The logical operator compares each element of the first vector with the
corresponding element of the second vector.
There are the following types of operators which are supported by R:

S. Operator Description Example


No

1. & This operator is known as the Logical AND a <- c(3, 0, TRUE,
2+2i)
operator. This operator takes the first element
b <- c(2, 4, TRUE,
of both the vector and returns TRUE if both the
elements are TRUE. 2+3i)
print(a&b)

It will give us the


following output:

[1] TRUE
FALSE TRUE TRUE

2. | This operator is called the Logical OR operator. a <- c(3, 0, TRUE,


2+2i)
This operator takes the first element of both the
b <- c(2, 4, TRUE,
vector and returns TRUE if one of them is TRUE. 2+3i)
print(a|b)

It will give us the


following output:

[1] TRUE
TRUE TRUE TRUE

3. ! This operator is known as Logical NOT operator. a <- c(3, 0, TRUE,


2+2i)
This operator takes the first element of the
print(!a)
vector and gives the opposite logical value as a
result. It will give us the
following output:

[1] FALSE
TRUE FALSE FALSE

4. && This operator takes the first element of both the a <- c(3, 0, TRUE,
2+2i)
vector and gives TRUE as a result, only if both
b <- c(2, 4, TRUE,
are TRUE. 2+3i)
print(a&&b)

It will give us the


following output:

[1] TRUE
5. || This operator takes the first element of both the a <- c(3, 0, TRUE,
2+2i)
vector and gives the result TRUE, if one of them
b <- c(2, 4, TRUE,
is true. 2+3i)
print(a||b)

It will give us the


following output:

[1] TRUE

Assignment Operators
An assignment operator is used to assign a new value to a variable. In R,
these operators are used to assign values to vectors. There are the following
types of assignment

S. Operator Description Example


No

1. <- or = or These operators are a <- c(3, 0, TRUE, 2+2i)


b <<- c(2, 4, TRUE, 2+3i)
<<- known as left
d = c(1, 2, TRUE, 2+3i)
assignment operators. print(a)
print(b)
print(d)

It will give us the following output:

[1] 3+0i 0+0i 1+0i 2+2i


[1] 2+0i 4+0i 1+0i 2+3i
[1] 1+0i 2+0i 1+0i 2+3i

2. -> or ->> These operators are c(3, 0, TRUE, 2+2i) -> a


c(2, 4, TRUE, 2+3i) ->> b
known as right
print(a)
assignment operators. print(b)

It will give us the following output:

[1] 3+0i 0+0i 1+0i 2+2i


[1] 2+0i 4+0i 1+0i 2+3i

operators which are supported by R:


Miscellaneous Operators
Miscellaneous operators are used for a special and specific purpose. These
operators are not used for general mathematical or logical computation.
There are the following miscellaneous operators which are supported in R

S. Operator Description Example


No

1. : The colon operator is used v <- 1:8


print(v)
to create the series of
numbers in sequence for a It will give us the following
vector. output:

[1] 1 2 3 4 5 6 7
8

2. %in% This is used when we want a1 <- 8


a2 <- 12
to identify if an element
d <- 1:10
belongs to a vector. print(a1%in%t)
print(a2%in%t)

It will give us the following


output:

[1] FALSE
[1] FALSE

3. %*% It is used to multiply a M=matrix(c(1,2,3,4,5,6),


nrow=2, ncol=3, byrow=TRUE)
matrix with its transpose.
T=m%*%T(m)
print(T)

It will give us the following


output:

14 32
32 77
R Switch Statement
A switch statement is a selection control mechanism that allows the value of
an expression to change the control flow of program execution via map and
search.
The switch statement is used in place of long if statements which compare a
variable with several integral values. It is a multi-way branch statement
which provides an easy way to dispatch execution for different parts of code.
This code is based on the value of the expression.
This statement allows a variable to be tested for equality against a list of
values. A switch statement is a little bit complicated. To understand it, we
have some key points which are as follows:
o If expression type is a character string, the string is matched to the listed
cases.
o If there is more than one match, the first match element is used.
o No default case is available.
o If no case is matched, an unnamed case is used.

There are basically two ways in which one of the cases is selected:

1) Based on Index
If the cases are values like a character vector, and the expression is
evaluated to a number than the expression's result is used as an index to
select the case.

2) Based on Matching Value


When the cases have both case value and output value like
["case_1"="value1"], then the expression value is matched against case
values. If there is a match with the case, the corresponding value is the
output.
The basic syntax of If-else statement is as follows:
switch(expression, case1, case2, case3....)

Example 1
x <- switch(
3,
"Shubham",
"Nishka",
"Gunjan",
"Sumit"
)
print(x)
Output:

For loop in R Programming


In R, a for loop is a way to repeat a sequence of instructions under certain
conditions. It allows us to automate parts of our code which need repetition.
In simple words, a for loop is a repetition control structure. It allows us to
efficiently write the loop that needs to execute a certain number of time.
In R, a for loop is defined as :
1. Instead of initializing and declaring a loop counter variable, we declare
a variable which is of the same type as the base type of the vector,
matrix, etc., followed by a colon, which is then followed by the array or
matrix name.
2. In the loop body, use the loop variable rather than using the indexed
array element.

There is a following syntax of for loop in R:


for (value in vector)
{
statements
}

Flowchart of For loop in R:

# R program to illustrate of for loop

# assigning strings to the vector

PROGRAM:
week < - c('Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday')
for (day in week)
{
# displaying each string in the vector
print(day)
}
Output:
[1] "Sunday"
[1] "Monday"
[1] "Tuesday"
[1] "Wednesday"
[1] "Thursday"
[1] "Friday"
[1] "Saturday"

In the above program, initially, all the days(strings) of the week are assigned to
the vector week. Then for loop is used to iterate over each string in a week. In
each iteration, each day of the week is displayed.

WHILE LOOP
While loop is used when the exact number of iterations of loop is not known
beforehand. It executes the same code again and again until a stop condition is
met. While loop checks for the condition to be true or false n+1 times rather
than n times. This is because the while loop checks for the condition before
entering the body of the loop.

R- While loop Syntax:


while (test_expression)
{
statement
update_expression
}

How does a While loop execute?


 Control falls into the while loop.
 The flow jumps to Condition
 Condition is tested.
 If Condition yields true, the flow goes into the Body.
 If Condition yields false, the flow goes outside the loop
 The statements inside the body of the loop get executed.
 Updation takes place.
 Control flows back to Step 2.
 The while loop has ended and the flow has gone outside.

R – while loop Flowchart:

WHILE LOOP PROGRAM

v <- c("Hello","while loop")


cnt <- 2

while (cnt < 7) {


print(v)
cnt = cnt + 1
}

When the above code is compiled and executed, it produces the following result −
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
LOOPING OVER LIST:
Looping over a list is just as easy and convenient as looping over a
vector. Looping over list has 3 ways

1) Loop Through List & Display All Sub-Elements on Same Line

#print each sub-element on same line


for (i in team_info) {
print(i)
}

output

[1] "Mavericks"
[1] "G" "F" "C"
[1] 3
.

2: Loop Through List & Display All Sub-Elements on Different Lines

#print each sub-element on different lines


for (i in team_info) {
for(j in i)
{print(j)}
}

output

[1] "Mavericks"
[1] "G"
[1] "F"
[1] "C"
[1] 3
3: Loop Through List & Only Display Specific Values

#only display first value in each element of list


for(i in 1:length(team_info)) {
print(team_info[[i]][1])
}

output

[1] "Mavericks"
[1] "G"
[1] 3

Types of vectors
Atomic vectors in R
In R, there are four types of atomic vectors. Atomic vectors play an important role in Data
Science. Atomic vectors are created with the help of c() function. These atomic vectors are
as follows:
Numeric vector
The decimal values are known as numeric data types in R. If we assign a decimal value to
any variable d, then this d variable will become a numeric type. A vector which contains
numeric elements is known as a numeric vector.
Example:

1. d<-45.5
2. num_vec<-c(10.1, 10.2, 33.2)
3. class(d)
4. num_vec
5. class(d)
6. class(num_vec)
Output
[1] 45.5
[1] 10.1 10.2 33.2
[1] "numeric"
[1] "numeric"

Integer vector
A non-fraction numeric value is known as integer data. This integer data is represented by
"Int." The Int size is 2 bytes and long Int size of 4 bytes. There is two way to assign an
integer value to a variable, i.e., by using as.integer() function and appending of L to the
value.
A vector which contains integer elements is known as an integer vector.
Example:

1. d<-as.integer(5)
2. e<-5L
3. int_vec<-c(1,2,3,4,5)
4. int_vec<-as.integer(int_vec)
5. int_vec1<-c(1L,2L,3L,4L,5L)
6. class(d)
7. class(e)
8. class(int_vec)
9. class(int_vec1)
Output
[1] "integer"
[1] "integer"
[1] "integer"
[1] "integer"

Character vector
A character is held as a one-byte integer in memory. In R, there are two different ways to
create a character data type value, i.e., using as.character() function and by typing string
between double quotes("") or single quotes('').
A vector which contains character elements is known as an integer vector.
Example:

1. d<-'shubham'
2. e<-"Arpita"
3. f<-“65”
4. f<-as.character(f)
5. d
6. e
7. f
8. char_vec<-c(1,2,3,4,5)
9. char_vec<-as.character(char_vec)
10. char_vec1<-c("shubham","arpita","nishka","vaishali")
11. char_vec
12. class(d)
13. class(e)
14. class(f)
15. class(char_vec)
16. class(char_vec1)
Output
[1] "shubham"
[1] "Arpita"
[1] "65"
[1] "1" "2" "3" "4" "5"
[1] "shubham" "arpita" "nishka" "vaishali"
[1] "character"
[1] "character"
[1] "character"
[1] "character"
[1] "character"

Logical vector
The logical data types have only two values i.e., True or False. These values are based on
which condition is satisfied. A vector which contains Boolean values is known as the logical
vector.
Example:

1. d<-as.integer(5)
2. e<-as.integer(6)
3. f<-as.integer(7)
4. g<-d>e
5. h<-e<f
6.
7. log_vec<-c(d<e, d<f, e<d,e<f,f<d,f<e)
8. log_vec
9. class(g)
10. class(h)
11. class(log_vec)
Output
[1] FALSE
[1] TRUE
[1] TRUE TRUE FALSE TRUE FALSE FALSE
[1] "logical"
[1] "logical"
[1] "logical"

Vector subsetting
In R Programming Language, subsetting allows the user to access elements from an object. It
takes out a portion from the object based on the condition provided. There are 4 ways of
subsetting in R programming. Each of the methods depends on the usability of the user and the
type of object.

Method 1: Subsetting in R Using [ ] Operator


Using the ‘[ ]’ operator, elements of vectors and observations from data frames can be accessed.
To neglect some indexes, ‘-‘ is used to access all other indexes of vector or data frame.

Program

# Create vector
x <- 1:15

# Print vector
cat("Original vector: ", x, "\n")

# Subsetting vector
cat("First 5 values of vector: ", x[1:5], "\n")

cat("Without values present at index 1, 2 and 3: ",


x[-c(1, 2, 3)], "\n")
Output:
Original vector: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
First 5 values of vector: 1 2 3 4 5
Without values present at index 1, 2 and 3: 4 5 6 7 8 9 10 11
12 13 14 15

Method 2: Subsetting in R Using [[ ]] Operator

[[ ]] operator is used for subsetting of list-objects. This operator is the same as [ ] operator but the
only difference is that [[ ]] selects only one element whereas [ ] operator can select more than 1
element in a single command.

# Create list
ls <- list(a = 1, b = 2, c = 10, d = 20)

# Print list
cat("Original List: \n")
print(ls)

# Select first element of list


cat("First element of list: ", ls[[1]], "\n")
Output:
Original List:
$a
[1] 1

$b
[1] 2

$c
[1] 10

$d
[1] 20

First element of list: 1

Method 3: Subsetting in R Using $ Operator

$ operator can be used for lists and data frames in R. Unlike [ ] operator, it selects only a single
observation at a time. It can be used to access an element in named list or a column in data
frame. $ operator is only applicable for recursive objects or list-like objects.

# Create list
ls <- list(a = 1, b = 2, c = "Hello", d = "GFG")

# Print list
cat("Original list:\n")
print(list)

# Print "GFG" using $ operator


cat("Using $ operator:\n")
print(list$d)
Output:
Original list:
$a
[1] 1

$b
[1] 2

$c
[1] "Hello"

$d
[1] "GFG"

Using $ operator:
[1] "GFG"

Method 4: Subsetting in R Using subset() Function

subset() function in R programming is used to create a subset of vectors, matrices, or data frames
based on the conditions provided in the parameters.
Syntax: subset(x, subset, select)
Parameters:
 x: indicates the object
 subset: indicates the logical expression on the basis of which subsetting has to be done
 select: indicates columns to select

R Data Frame
A data frame is a two-dimensional array-like structure or a table in which a column contains
values of one variable, and rows contains one set of values from each column. A data frame
is a special case of the list in which each component has equal length.
A data frame is used to store data table and the vectors which are present in the form of a
list in a data frame, are of equal length.
In a simple way, it is a list of equal length vectors. A matrix can contain one type of data, but
a data frame can contain different data types such as numeric, character, factor, etc.
There are following characteristics of a data frame.

o The columns name should be non-empty.


o The rows name should be unique.
o The data which is stored in a data frame can be a factor, numeric, or character type.
o Each column contains the same number of data items.

How to create Data Frame


In R, the data frames are created with the help of frame() function of data. This function
contains the vectors of any type such as numeric, character, or integer. In below example,
we create a data frame that contains employee id (integer vector), employee
name(character vector), salary(numeric vector), and starting date(Date vector).
Example

# Creating the data frame.


emp.data<- data.frame( employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,915.2,611.0,729.0,843.25),

starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",


"2015-03-27")),
stringsAsFactors = FALSE
)
# Printing the data frame.
print(emp.data)
Output
employee_idemployee_namesalstarting_date
1 1 Shubham623.30 2012-01-01
2 2 Arpita915.20 2013-09-23
3 3 Nishka611.00 2014-11-15
4 4 Gunjan729.00 2014-05-11
5 5 Sumit843.25 2015-03-27

R Lists
In R, lists are the second type of vector. Lists are the objects of R which contain elements of
different types such as number, vectors, string and another list inside it. It can also contain a
function or a matrix as its elements. A list is a data structure which has components of
mixed data types. We can say, a list is a generic vector which contains other objects.

Example
vec <- c(3,4,5,6)
char_vec<-c("shubham","nishka","gunjan","sumit")
logic_vec<-c(TRUE,FALSE,FALSE,TRUE)
out_list<-list(vec,char_vec,logic_vec)
Output:
[[1]]
[1] 3 4 5 6
[[2]]
[1] "shubham" "nishka" "gunjan" "sumit"
[[3]]
[1] TRUE FALSE FALSE TRUE

Lists creation
The process of creating a list is the same as a vector. In R, the vector is created with the
help of c() function. Like c() function, there is another function, i.e., list() which is used to
create a list in R. A list avoid the drawback of the vector which is data type. We can add the
elements in the list of different data types.
syntax

list()
Example 1: Creating list with same data type

list_1<-list(1,2,3)
list_2<-list("Shubham","Arpita","Vaishali")
list_3<-list(c(1,2,3))
list_4<-list(TRUE,FALSE,TRUE)
list_1
list_2
list_3
list_4
Output:
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3

[[1]]
[1] "Shubham"
[[2]]
[1] "Arpita"
[[3]]
[1] "Vaishali"

[[1]]
[1] 1 2 3

[[1]]
[1] TRUE
[[2]]
[1] FALSE
[[3]]
[1] TRUE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy